【问题标题】:MVC, disable multiple form submissionsMVC,禁用多个表单提交
【发布时间】:2014-06-22 00:04:46
【问题描述】:

我有以下观点,如果提交提交按钮被垃圾邮件发送,我怎么能不允许用户只提交一次表单

    <h2>Add new Storage</h2>
<br /><br />
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)


        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DateFrom)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateFrom)
            @Html.ValidationMessageFor(model => model.DateFrom)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DateTo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateTo)
            @Html.ValidationMessageFor(model => model.DateTo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Size)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Size)
            @Html.ValidationMessageFor(model => model.Size)
        </div>
        <br />
        <p>
            <input type="submit" value="Create" />
        </p>
}
<br />
<div>
    @Html.ActionLink("View all storage", "ViewStorage")
</div>

控制器:

[HttpGet]
        public ActionResult Add()
        {
            ViewBag.Storage = new BusinessLayer.Storage().getAllStorage();
            return View();
        }

        [HttpPost]
        public ActionResult Add(Models.ActivityModel activity)
        {
            if (activity.Storage != null)
            {
                if (new BusinessLayer.Activities().addActivity(activity.Storage, activity.Name, activity.Date, activity.Keywords))
                {
                    return RedirectToAction("Home", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "An error occurred, please try again!");
            }

            ViewBag.Storage = new BusinessLayer.Storage().getAllStorage();
            return View(activity);
        }

【问题讨论】:

    标签: asp.net-mvc forms submit


    【解决方案1】:

    没有适当的方法来检测您的表单是否被有意提交多次,您应该组织您的业务模型,使其不会验证/接受重复或任何无效输入。

    类似这样的:

    public ActionResult Add(Models.ActivityModel activity)
            {
                if (activity.Storage != null && validator.isValid(activity.Storage))
                {
                    if (new BusinessLayer.Activities().addActivity(activity.Storage, activity.Name, activity.Date, activity.Keywords))
                    {
                        return RedirectToAction("Home", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "An error occurred, please try again!");
                }
    
                ViewBag.Storage = new BusinessLayer.Storage().getAllStorage();
                return View(activity);
            }
    

    validator.isValid(Models.ActivityModel.Storage storage) 检查重复项、有效输入等。 另一个与您相关的问题是表单重新提交,当表单在每次页面刷新时多次提交时,您可以通过实现Post-Redirect-Get pattern

    来解决它

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      相关资源
      最近更新 更多