当我尝试编辑 Duty 对象时,我的 Controller DutyController.cs
中的行 ctx.SaveChanges();
抛出以下异常。
System.Data.Services.Client.DataServiceClientException:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code></code>
<message xml:lang="en-US">Resource not found for the segment 'Duty'.</message>
</error>
我的编辑职责 View 包括场合下拉列表。提交时,OccasionId guid 已成功绑定(bind)到 Duty 模型。
模型\Duty.cs
[MetadataType(typeof(DutyMetadata))]
public partial class Duty
{
private class DutyMetadata
{
...
[Required]
[UIHint("OccasionId")]
[Display(Name = "Occasion")]
public object OccasionId { get; set; }
}
}
Views\Duty\Edit.aspx
<%: Html.EditorForModel() %>
Views\Shared\DisplayTemplates\OccasionId.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Guid>" %>
<%: Html.DropDownList(string.Empty, new SelectList(ViewData["occasions"] as IEnumerable<Web.VolunteerData.Occasion>, "Id", "Designation", Model), "Select Occasion")%>
Controllers\DutyController.cs
//
// GET: /Duty/Edit/5
public ActionResult Edit(Guid id)
{
var Model = (from Duty d in ctx.Duties where d.Id == id select d).Single();
ViewData["occasions"] = from Occasion o in ctx.Occasions orderby o.Designation select o;
return View(Model);
}
//
// POST: /Duty/Edit/5
[HttpPost]
public ActionResult Edit(Duty Model)
{
ctx.AttachTo("Duty", Model);
ctx.UpdateObject(Model);
ctx.SaveChanges();
return RedirectToAction("Index");
}
为什么 SaveChanges() 会导致“找不到该段的资源...”异常?
有更好的方法让我做我想做的事情吗?
最佳答案
我认为这是另一个问题的重复,但无论如何。问题出在 AttachTo 调用中。该方法的第一个参数是实体集的名称(而不是实体类型)。在您的情况下,实体集似乎被称为“职责”。因此,只需使用“Duties”作为 AttachTo 的参数,它就应该可以工作。 下次请发布完整的异常消息,因为这很可能包括有问题的“段”的名称,并且会使问题更加清晰。
关于c# - WCF 数据服务 SaveChanges() : "Resource not found for the segment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3086115/