I have an ASP.NET MVC application, which does the next:
- Shows a form with a table in there, and some fields per row to be filled
- Validates the input in the fields via server validation
- A submit button, which when user clicks on it, process the request in the server (long time process, > 5 seconds)
Having such scenario, I've implemented:
- Razor view with the proper logic to show the form
- Controller to receive the action addressed by the Submit button
- Model with the properties to exchange between controller and view
I would like to fulfill the next requirement:
-When Submit button is clicked
- EITHER disable it (till success or error)
- OR show a modal progress bar (till success or error)
All these, in order to avoid the user to click again in the submit button while the server is processing the request.
If I disable the button via Javascript , the controller post action is never called, so disabling it via Javascript is not a solution
Implementing the PRG pattern does help, as the button is still enabled.
Controller:
public ActionResult Index()
{
var viewModel = Initialize();
return View(viewModel);
}
[HttpPost]
public ActionResult HandleErrors(List<elements> entries)
{
var viewModel = new MyViewModel();
entries.ForEach(entry =>
{
//Add validation errors to UI via ModelState when applies
//validate CorrectionInfo
if (string.IsNullOrEmpty(entry.Property1))
{
ModelState.AddModelError("Property1" + entry.Key, "Correction info is required");
}
//more validations ......
});
if (ModelState.IsValid)
{
//save changes in DB
if (DataBaseManager.SaveChanges(entries))
{
return RedirectToAction("Index", "MyController");
}
else // there were errors saving entries into the DB
{
viewmodel.Error = true;
return View("Index", viewModel);
}
}
else //ModelState is not valid
{
viewModel.ValidationErrors = true;
return View("Index", viewModel);
}
}
View:
using (Html.BeginForm("HandleErrors", "MyController", FormMethod.Post))
{
<table id="filesTable" class="table table-striped table-bordered">
<thead>
<tr>
<td>Column 1</td>
<td>Column 2</td>
...
...
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Entries.Count; i++)
{
<tr>
<td>
@Model.Entries[i].Property1
</td>
<td>
@Model.Entries[i].Property2
</td>
...
...
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="4">
<input type="submit" value="Submit" />
</td>
</tr>
</tfoot>
</table>
}
Thank you!!