如何使用 ASP.NET MVC 2 编辑 WCF 数据服务对象?
我不想在控制器中添加不必要的代码。
这有效:
//
// 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>
为什么?我应该如何编写这?
I don't want to put any more code into my controller than I have to.
This works:
//
// 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");
}
But, I don't want to have to type every property.
This fails:
//
// POST: /Duty/Edit/5
[HttpPost]
public ActionResult Edit(Duty Model)
{
ctx.AttachTo("Duty", Model);
ctx.UpdateObject(Model);
ctx.SaveChanges();
return RedirectToAction("Index");
}
It throws a 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>
Why? How should I write this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的代码猜测实体集实际上称为“职责”。所以你的代码应该是这样的:
//
// POST: /Duty/Edit/5
(AttachTo 方法的第一个参数是实体集名称,而不是实体类型的名称。)
请注意,为了使其正常工作,您必须确保服务器上已存在相关实体(即具有相同键属性值的实体)。这将向该实体发出 PUT 请求,如果该实体不存在,则会失败并返回 404。
Guessing from your code the entity set is actually called "Duties". So your code should look like:
//
// POST: /Duty/Edit/5
(The first parameter for the AttachTo method is the entity set name, not the name of the entity type.)
Note that in order for this to work, you must be sure that the entity in question already exists on the server (that is entity with the same key property values). This will issue a PUT request to that entity, and if it doesn't exist it will fail with 404.
尝试一下它可能会起作用:
这将告诉数据上下文每个属性都已更新。
try this it might work:
This will tell data context that every property has been updated.