[英]ASP.NET MVC Redirect
I work on one of my first ASP MVC-programs at the moment. 我目前正在研究我的第一个ASP MVC程序之一。 The program should show me a list of product, and with a link beneath the name of the product it should be possible to edit the product. 该程序应显示产品列表,并在产品名称下方带有链接,以便可以编辑产品。 No problem so far. 到目前为止没有问题。
@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>
}
Now I have written a Save
action method that should update my data: 现在,我编写了一个Save
操作方法,该方法应该更新我的数据:
public ActionResult Save(Product p)
{
ProductRepository rep = new ProductRepository();
rep.Update(p);
return RedirectToAction("List");
}
The "List"-View is where I can see all products with the edit-link. 我可以在“列表”视图中查看带有编辑链接的所有产品。 The problem is, that if I press the save-button, it redirects me to the old list, not to the updated one. 问题是,如果我按保存按钮,它将把我重定向到旧列表,而不是更新的列表。 I debugged my project and I´m sure that the update-method works correct and updates the product. 我调试了项目,并且确定更新方法可以正确工作并更新产品。
My List
action is: 我的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>
Because you asked, here is the List
Action: 由于您的询问,这是List
操作:
public ActionResult List()
{
ProductRepository rep = new ProductRepository();
return View(rep.GetAll());
}
So where could be my mistake? 那么我的错误可能在哪里呢?
It looks like you're calling the update, but you're not actually submitting the transaction itself, does your repository have a SubmitChanges, AcceptChanges or Commit or something similar? 看起来您在调用更新,但是您实际上不是在提交事务本身,您的存储库中是否有SubmitChanges,AcceptChanges或Commit或类似的东西? As with DataTables, your changes won't actually take effect (save to the database) until you call AcceptChanges. 与DataTables一样,更改必须在您调用AcceptChanges之前才真正生效(保存到数据库)。
Try include an HttpPost
attribute at Save
controller method. 尝试在“ 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.