我正在 mvc 4.0 中的提交事件上提交表单。提交表单后,将发布到其操作并创建记录。创建记录后,当我刷新页面时,会创建另一个重复记录。
我已经使用了以下但没有成功:
ModelState.Clear();
ModelState.SetModelValue("Key", new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture));
ModelState.Remove("Key");
我不想使用 AJAX 表单 Post,也不想在另一个页面上重定向。
有没有什么方法可以像mvc4.0中的!Page.IsPostBack()
一样在asp.net中使用。
我也不想使用 session 。
(微软吹嘘MVC,MVC没有像asp.net那样的任何 View 状态,但现在我不这么认为)。
最佳答案
成功更新索引操作后,您可以重定向。在这种情况下,刷新将重新发送请求到索引操作,而不是发布到更新操作。此模式称为“Post-Redirect-Get”模式。 示例:
[HttpPost]
public ActionResult Update(SomeModelViewClass model)
{
//some code that save data to db
return RedirectToAction("index", new {id=model.Id});
}
[HttpGet]
public ActionResult Index(Guid id)
{
//some code that get data from db by id
return View(model);
}
关于asp.net-mvc - 在 MVC 中发布表单后,如何防止页面刷新时数据库中出现重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15340001/