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?