c# - WCF 数据服务 SaveChanges() : "Resource not found for the segment"

标签 c# asp.net-mvc-2 wcf-data-services

当我尝试编辑 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/

相关文章:

c# - 替代多个 if 语句的 Lambda 表达式

c# - 以编程方式将 ListViewItem 添加到 WPF 中的 Listview

asp.net - 如何在 ASP.NET MVC 中使用 Html.Action() 将参数传递给 Action?

网站内的asp.net mvc 2 web 应用程序?

c# - 在 web.config 中添加第三个端点显示错误

entity-framework - EF4 0..1 关系导致错误 2016 : Condition cannot be specified

linq - WCF 数据服务支持的 Linq

c# - 需要一些关于排序字符串列表的想法

c# - .NET Core 项目引用问题 - "Cannot find project info for ' XXX.csproj'”

ASP.NET MVC 2 - 如何使用 IgnoreRoute 忽略整个目录?