我的观点如下:DemoView.cshtml
@model Mvc3Razor.Models.UserModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>UserModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<p>
<input type="submit" value="Create" /></p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script type="text/javascript">
$(function () {
$('input[type="submit"]').click(function () {
$(this).attr('disabled', 'disabled');
});
});
</script>
一个控制器:HomeController.cs
[HttpPost]
public ViewResult Create(UserModel um)
{
if (!TryUpdateModel(um))
{
ViewBag.updateError = "Create Failure";
return View(um);
}
_usrs.Create(um);
return View("Details", um);
}
为了防止多次点击按钮,我试图在用户点击“创建”按钮后禁用该按钮,但是一旦禁用“创建”按钮,它就不会触发HttpPost方法。
有人可以帮我解决这个问题吗?
答案 0 :(得分:4)
尝试在提交表单时禁用该按钮,而不是在单击提交按钮时禁用该按钮:
$("form").submit(function() {
$(this).attr("disabled", "disabled");
return true;
});
答案 1 :(得分:0)
我建议在成功发布后将用户重定向到Index Page。您想使用PRG Pattern
Post, Redirect, Get (PRG) pattern“有助于避免 重复表单提交并允许Web应用程序表现更多 直观地使用浏览器书签和重新加载按钮“
usrs.Create(um);
//Redirect to Index Page here
有关详细信息,请参阅此SO question