我有一个ASP.NET MVC应用程序,它执行下一步:
有了这样的场景,我已经实现了:
我想满足下一个要求:
- 点击提交按钮时
所有这些,以避免用户在服务器处理请求时再次单击提交按钮。
如果我通过Javascript禁用按钮,则永远不会调用控制器发布操作,因此通过Javascript禁用它不是解决方案
实施 PRG模式确实有帮助,因为按钮仍处于启用状态。
控制器:
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);
}
}
查看:
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>
}
谢谢!