我在mvc 4.0中提交一个提交事件的表单。提交表单后,将其发送到其操作并创建记录。创建记录后,当我刷新页面,然后另一个重复的记录被创建。
我已经使用了以下命令,但是没有成功:
ModelState.Clear();
ModelState.SetModelValue("Key", new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture));
ModelState.Remove("Key");
我不想使用AJAX表单Post,也不想重定向到另一个页面。
我们是否有办法在asp.net中使用像mvc4.0中的!Page.IsPostBack()
。
我不想使用会话也
(微软吹MVC MVC没有任何视图状态像asp.net,但现在我不这么认为)。
可以在更新成功后重定向到索引操作。在这种情况下,刷新将重新发送请求到索引操作,而不是发送到更新操作。这种模式被称为"后重定向-获取"模式。例子:
[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);
}
您可以使用Ajax.post提交表单。像下面这样构造表单标签。
@using (@Html.BeginForm("Index", "ControllerName", FormMethod.Post, new { id = "anyFormName" }))
从页面调用ajax post。
$.ajax({
url: "/ControllerName/Index",
type: "POST",
data: $("#anyFormName").serialize(),
success: function (data) {
$("#divFormContainer").html(data);
}
});
在控制器中创建Index方法,如下所示。
[HttpPost]
public ActionResult Index(FormCollection fc)
{
// Put the form post logics here....
// Build the model for the view...
return PartialView("Index", model);
}