gpt4 book ai didi

asp.net-mvc - 在MVC中发布表单后如何防止在页面刷新时在数据库中重复输入

转载 作者:行者123 更新时间:2023-12-04 05:07:28 25 4
gpt4 key购买 nike

我正在 mvc 4.0 中提交关于提交事件的表单。提交表单后发布到其操作并创建记录。在我刷新页面时创建记录后,会创建另一个重复记录。

我已经使用了以下但没有成功:

ModelState.Clear();
ModelState.SetModelValue("Key", new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture));
ModelState.Remove("Key");

我不想使用 AJAX 表单 Post,也不想重定向到另一个页面。

我们在asp.net 中有什么方法可以使用,例如 !Page.IsPostBack()在 mvc4.0 中。

我也不想使用 session 。

(微软吹嘘 MVC MVC 没有像 asp.net 这样的任何 View 状态,但现在我不这么认为)。

最佳答案

您可以使用 Ajax.post 提交表单。构建您的表单标签,如下所示。

@using (@Html.BeginForm("Index", "ControllerName", FormMethod.Post, new { id = "anyFormName" }))

从页面调用ajax帖子。
$.ajax({
url: "/ControllerName/Index",
type: "POST",
data: $("#anyFormName").serialize(),
success: function (data) {
$("#divFormContainer").html(data);
}
});

在 Controller 中创建索引方法,如下所示。
[HttpPost]
public ActionResult Index(FormCollection fc)
{
// Put the form post logics here....
// Build the model for the view...
return PartialView("Index", model);
}

关于asp.net-mvc - 在MVC中发布表单后如何防止在页面刷新时在数据库中重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15340001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com