[英]ASP.NET MVC Redirect
我目前正在研究我的第一个ASP MVC程序之一。 该程序应显示产品列表,并在产品名称下方带有链接,以便可以编辑产品。 到目前为止没有问题。
@model MVC3Demo.Product
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm("Save", "Product"))
{
<div>
<input type="hidden" id="ID" name="ID" value="@Model.ID" />
ProduktID @Model.ID
</div>
<div>
Produktname <input id="Name" name="Name" type="text" value=@Model.Name />
</div>
<div>
Preis <input id="Price" name="Price" type="text" value=@Model.Price />
</div>
<div>
<input type="submit" value="Speichern"/>
</div>
}
现在,我编写了一个Save
操作方法,该方法应该更新我的数据:
public ActionResult Save(Product p)
{
ProductRepository rep = new ProductRepository();
rep.Update(p);
return RedirectToAction("List");
}
我可以在“列表”视图中查看带有编辑链接的所有产品。 问题是,如果我按保存按钮,它将把我重定向到旧列表,而不是更新的列表。 我调试了项目,并且确定更新方法可以正确工作并更新产品。
我的List
操作是:
@model IEnumerable<MVC3Demo.Product>
@{
ViewBag.Title = "List";
}
<h2>List</h2>
<ul>
@foreach (MVC3Demo.Product p in Model)
{
<li>@p.Name @Html.ActionLink("bearbeiten", "Edit", "Product", p, null)</li> //new{ ID = p.id}
}
</ul>
由于您的询问,这是List
操作:
public ActionResult List()
{
ProductRepository rep = new ProductRepository();
return View(rep.GetAll());
}
那么我的错误可能在哪里呢?
看起来您在调用更新,但是您实际上不是在提交事务本身,您的存储库中是否有SubmitChanges,AcceptChanges或Commit或类似的东西? 与DataTables一样,更改必须在您调用AcceptChanges之前才真正生效(保存到数据库)。
尝试在“ Save
控制器方法中包含HttpPost
属性。
[HttpPost]
public ActionResult Save(Product p)
{
ProductRepository rep = new ProductRepository();
rep.Update(p);
return RedirectToAction("List");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.