0

I have the following view, how can I not allow the user to submit the form ONLY once if the submit submit button was spammed

    <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>

Controller:

[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);
        }
tereško
  • 58,060
  • 25
  • 98
  • 150
rikket
  • 2,357
  • 7
  • 46
  • 74

1 Answers1

0

there is no proper way to detect if your form gets submitted multiple times intentionally or not, you should organize your business model so that it does not validate/accept duplicate or whatever invalid input.

something like this :

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);
        }

where validator.isValid(Models.ActivityModel.Storage storage) checks for duplicates, valid input and so on. Another problem, that's related to yours, is form resubmission, when form get submitted multiple times on every page refresh, you fix it by implementing Post-Redirect-Get pattern

Community
  • 1
  • 1
Eugen Halca
  • 1,775
  • 2
  • 13
  • 26