如何使用 ASP.NET MVC 2 编辑 WCF 数据服务对象?

发布于 2024-09-07 03:29:57 字数 1373 浏览 6 评论 0原文

我不想在控制器中添加不必要的代码。

有效:

    //
    // 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-09-14 03:29:57

从您的代码猜测实体集实际上称为“职责”。所以你的代码应该是这样的:
//
// 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。

Guessing from your code the entity set is actually called "Duties". So your code should look like:
//
// POST: /Duty/Edit/5

[HttpPost] 
public ActionResult Edit(Duty Model) 
{ 
    ctx.AttachTo("Duties", Model); 
    ctx.UpdateObject(Model); 
    ctx.SaveChanges(); 
    return RedirectToAction("Index"); 
} 

(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.

埋情葬爱 2024-09-14 03:29:57

尝试一下它可能会起作用:

ctx.AttachUpdated(Model);
ctx.SaveChanges();

这将告诉数据上下文每个属性都已更新。

try this it might work:

ctx.AttachUpdated(Model);
ctx.SaveChanges();

This will tell data context that every property has been updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文