![](/img/trans.png)
[英]How do I pass a list of objects from a WCF service to an ASP.NET web app
[英]How do I edit WCF Data Service objects with ASP.NET MVC 2?
我不想在控制器中放入不必要的代碼。
這有效:
//
// POST: /Duty/Edit/5
[HttpPost]
public ActionResult Edit(Duty Model)
{
Duty Attached = (from Duty d in ctx.Duties
where d.Id == Model.Id
select d).Single();
Attached.Designation = Model.Designation;
Attached.Instruction = Model.Instruction;
Attached.OccasionId = Model.OccasionId;
Attached.Summary = Model.Summary;
ctx.UpdateObject(Attached);
ctx.SaveChanges();
return RedirectToAction("Index");
}
但是,我不需要鍵入每個屬性。
這將失敗:
//
// POST: /Duty/Edit/5
[HttpPost]
public ActionResult Edit(Duty Model)
{
ctx.AttachTo("Duty", Model);
ctx.UpdateObject(Model);
ctx.SaveChanges();
return RedirectToAction("Index");
}
它引發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>
為什么? 我應該怎么寫呢?
試試這個可能會起作用:
ctx.AttachUpdated(Model);
ctx.SaveChanges();
這將告訴數據上下文每個屬性都已更新。
根據您的代碼猜測,實體集實際上稱為“職責”。 因此您的代碼應類似於:// // POST:/ Duty / Edit / 5
[HttpPost]
public ActionResult Edit(Duty Model)
{
ctx.AttachTo("Duties", Model);
ctx.UpdateObject(Model);
ctx.SaveChanges();
return RedirectToAction("Index");
}
(AttachTo方法的第一個參數是實體集名稱,而不是實體類型的名稱。)請注意,為了使其起作用,必須確保所涉及的實體在服務器上已經存在(即,實體具有相同的鍵屬性值)。 這將向該實體發出PUT請求,如果該請求不存在,它將失敗404。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.