0

我的看法如下: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 方法。

谁能帮我解决这个问题?

4

2 回答 2

4

尝试在提交表单时禁用按钮,而不是在单击提交按钮时禁用:

$("form").submit(function() {
    $(this).attr("disabled", "disabled");
    return true;
});
于 2012-06-12T14:26:32.293 回答
0

我建议在成功发布后将用户重定向回索引页面。你想使用PRG 模式

发布、重定向、获取 (PRG) 模式,其中“帮助避免重复的表单提交并允许 Web 应用程序通过浏览器书签和重新加载按钮更直观地表现”

usrs.Create(um);
//Redirect to Index Page here

有关更多详细信息,请参阅此SO 问题

于 2012-06-12T14:47:25.810 回答