ASP.NET MVC3 编辑操作不更新数据库中的记录
我配置了一个编辑操作,该操作不会更新记录,但不会引发任何异常。更新似乎有效,但更改并未反映在数据库中。
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案是因为您在无状态环境中工作,除非您使用自跟踪实体,否则您需要将对象附加到 EF 的图表。
不过,我过去在编辑方面遇到过问题。所以我最终去了数据库首先获取对象,然后合并我需要的更改,然后进行保存。
您需要将创建/编辑操作分开。只需检查 ID 是否 > 0 认为编辑还不够,请更明确。
总结一下:
总的来说,这是一种痛苦。许多开发人员(包括我自己)都经历过您所经历的事情。
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:
Overall, it's a pain. Many developers (myself included) have gone through what you have.