开发者

WCF Data Services SaveChanges(): "Resource not found for the segment"

开发者 https://www.devze.com 2023-01-04 09:45 出处:网络
My line ctx.SaveChanges(); in my controller DutyController.cs throws the following exception when I try to edit my Duty object.

My line ctx.SaveChanges(); in my controller DutyController.cs throws the following exception when I try to edit my Duty object.

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>

My Edit Duty view includes a drop down list of Occasions. The OccasionId guid is successfully bound to the Duty model on submit.

Models\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");
}

Why does SaveChanges() cause the "Resource not found for the segment..." exception?

Is there a better way for me to do what I'm trying to do?


I think this is a duplicate of another question here, but anyway. The problem is in the AttachTo call. The first parameter to that method is the name of the entity set (not the entity type). In your case the entity set seems to be called "Duties". So just use "Duties" as the parameter to the AttachTo and it should work. Next time please post the entire exception message as that very likely includes the name of the offending "Segment" and would make the problem much clearer.

0

精彩评论

暂无评论...
验证码 换一张
取 消