ASP.NET MVC3 编辑操作不更新数据库中的记录

发布于 2024-11-19 00:28:43 字数 2549 浏览 2 评论 0原文

我配置了一个编辑操作,该操作不会更新记录,但不会引发任何异常。更新似乎有效,但更改并未反映在数据库中。

我正在使用 EF 和 MVC3,我有一个定义保存客户的接口

 public interface ICustomerRepository
 {
    //allows a sequence of customers to be displayed
    IQueryable<Customer> Customers { get; }
    //saves edits to customer records
    void SaveCustomer(Customer customer);
 }

然后我的实现

public void SaveCustomer(Customer customer) 
    {
        if (customer.CustomerId == 0)
        {
            context.Customers.Add(customer);
        }
        context.SaveChanges();
    }

然后在控制器中我的 get 和 post 操作

public ViewResult Edit(int customerId)
    {
        Customer customer = repository.Customers.FirstOrDefault(c => c.CustomerId ==   customerId);
        return View(customer);
    }

 [HttpPost]
    public ActionResult Edit(Customer customer)
    {
        if (ModelState.IsValid)
        {
            repository.SaveCustomer(customer);
            TempData["message"] = string.Format("{0} has been saved",    customer.CustomerName);
            return RedirectToAction("Index");
        }
        else
        {
            //there is something wrong with the data values
            return View(customer);
        }
    }

然后在我看来

 @model CustomerOrders.Domain.Entities.Customer

@{
ViewBag.Title = "Admin: Edit" + @Model.CustomerName;
Layout = "~/Views/Shared/_AdminLayout.cshtml";
 }

 <h1>Edit @Model.CustomerName</h1>

using (Html.BeginForm("Edit", "Admin"))
{
<div class="left-column">
<div class="editor">@Html.EditorFor(model => model.CustomerId)</div>
<div class="label-for">@Html.LabelFor(model => model.CustomerName)</div>
<div class="editor">@Html.EditorFor(model => model.CustomerName)</div>
@Html.ValidationMessageFor(model => model.CustomerName)
</div>
<div class="middle-column">
<div class="label-for">@Html.LabelFor(model => model.PrimaryContactName)</div>
<div class="editor">@Html.EditorFor(model => model.PrimaryContactName)</div>
<div class="label-for">@Html.LabelFor(model => model.PrimaryContactNo)</div>
<div class="editor">@Html.EditorFor(model => model.PrimaryContactNo)</div>
</div>
<div class="right-column">
<input type="submit" value="Save" />
@Html.ActionLink("Cancel and return to list", "Index")
</div>
}

我还有一个创建方法连接到相同的编辑操作和视图效果很好。不确定我哪里出了问题,我是 MVC3 新手,不确定我的编辑实现是否正确?

I have an edit action configured that is not updating records but not throwing any exceptions. The update appears to have worked but the changes are not reflected in the database.

I am using EF and MVC3, i have an interface that defines save customer

 public interface ICustomerRepository
 {
    //allows a sequence of customers to be displayed
    IQueryable<Customer> Customers { get; }
    //saves edits to customer records
    void SaveCustomer(Customer customer);
 }

Then my implementation of this

public void SaveCustomer(Customer customer) 
    {
        if (customer.CustomerId == 0)
        {
            context.Customers.Add(customer);
        }
        context.SaveChanges();
    }

Then in the controller my get and post actions

public ViewResult Edit(int customerId)
    {
        Customer customer = repository.Customers.FirstOrDefault(c => c.CustomerId ==   customerId);
        return View(customer);
    }

 [HttpPost]
    public ActionResult Edit(Customer customer)
    {
        if (ModelState.IsValid)
        {
            repository.SaveCustomer(customer);
            TempData["message"] = string.Format("{0} has been saved",    customer.CustomerName);
            return RedirectToAction("Index");
        }
        else
        {
            //there is something wrong with the data values
            return View(customer);
        }
    }

Then in my view i have

 @model CustomerOrders.Domain.Entities.Customer

@{
ViewBag.Title = "Admin: Edit" + @Model.CustomerName;
Layout = "~/Views/Shared/_AdminLayout.cshtml";
 }

 <h1>Edit @Model.CustomerName</h1>

using (Html.BeginForm("Edit", "Admin"))
{
<div class="left-column">
<div class="editor">@Html.EditorFor(model => model.CustomerId)</div>
<div class="label-for">@Html.LabelFor(model => model.CustomerName)</div>
<div class="editor">@Html.EditorFor(model => model.CustomerName)</div>
@Html.ValidationMessageFor(model => model.CustomerName)
</div>
<div class="middle-column">
<div class="label-for">@Html.LabelFor(model => model.PrimaryContactName)</div>
<div class="editor">@Html.EditorFor(model => model.PrimaryContactName)</div>
<div class="label-for">@Html.LabelFor(model => model.PrimaryContactNo)</div>
<div class="editor">@Html.EditorFor(model => model.PrimaryContactNo)</div>
</div>
<div class="right-column">
<input type="submit" value="Save" />
@Html.ActionLink("Cancel and return to list", "Index")
</div>
}

I also have a create method wired up to the same edit action and view which works fine. Not sure where i am going wrong, im new to MVC3 and not sure if my edit implementation is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魄砕の薆 2024-11-26 00:28:43

简单的答案是因为您在无状态环境中工作,除非您使用自跟踪实体,否则您需要将对象附加到 EF 的图表。

不过,我过去在编辑方面遇到过问题。所以我最终去了数据库首先获取对象,然后合并我需要的更改,然后进行保存。

您需要将创建/编辑操作分开。只需检查 ID 是否 > 0 认为编辑还不够,请更明确。

总结一下:

  1. 为新对象提供一种操作方法。在这种情况下使用 context.AddObject。
  2. 有另一个修改对象的操作方法。从存储库中获取对象,合并您的更改(从左到右,或自动映射器,或 TryUpdateModel),然后执行 context.SaveChanges。

总的来说,这是一种痛苦。许多开发人员(包括我自己)都经历过您所经历的事情。

The simple answer is because your working in a stateless environment, unless your using self tracking entities, you need to Attach the object to EF's graph.

I've had problems with edit in the past though. So i ended up going to the DB to fetch the object first, then merging in the changes i need, then doing Save.

You need to seperate your Create/Edit actions out. Simply checking if the ID > 0 to deem an edit is not enough, be more explicit.

So to sum up:

  1. Have one action method for new objects. Use context.AddObject in this scenario.
  2. Have another action method for modifying objects. Go get the object from the repository, merge in your changes (left to right, or auto mapper, or TryUpdateModel), and do context.SaveChanges.

Overall, it's a pain. Many developers (myself included) have gone through what you have.

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