我在我的控制器以下操作:location.reload删除的ModelState错误
[HttpPost]
public JsonResult RedirectToAspReportViewer(MvcReportPeriodSelectionViewModel periodFilter, MvcMultipleLocationSelectionViewModel locationFilter)
{
var jsonObject = new { HasErrors = false, strUrl = "/ASPReports/TreatmentOutcome.aspx" };
if (ModelState.IsValid)
{
try
{
//some code
}
catch (ValidationException ex)
{
this.HandleValidationErrors(ex);
jsonObject = new { HasErrors = true, strUrl = "/TreatmentOutcomeReport/Report/" };
}
}
return Json(jsonObject);
}
然后,在我的JavaScript,我有以下的功能,其中获得被称为我的ajax开机自检的的onSuccess功能。
正如你可以看到从上面,如果我的效应初探对象有错误,我想留在当前页面,只刷新它,这样我的ModelState错误仍然显示。
我面临的问题是当我拨打location.reload
时,我的模型状态错误不会显示在我的页面上。我感觉这是因为我再次发布到服务器,并且ModelState被清除。
我该如何避免这种情况?
UPDATE 我不能将验证错误添加到JsonResult,并在客户端更新必要的DOM以显示错误。在所有的我的看法,我有以下的共享视图返回我的错误:下面是示例:
@model ModelStateDictionary
@{
var cls = "alert-danger";
var type = ViewBag.Type;
if (type != null && type.ToString().ToLower() == "warning")
{
cls = "alert-warning";
}
var message = "Please attend to the following problems:";
if (ViewBag.Message != null && ViewBag.Message.ToString().Trim() != "")
{
message = ViewBag.Message.ToString().Trim();
}
}
@if (ViewData.ModelState.Keys.Any(k => ViewData.ModelState[k].Errors.Count() > 0))
{
<div class="alert @cls">
<button class="close" data-dismiss="alert" aria-hidden="true">× </button>
@Html.ValidationSummary(false, message)
</div>
}
这将调用在我所有的观点如下顶部:
<div id="valSummary">
@Html.Partial("_ValidationSummaryDisplay", ViewData.ModelState)
</div>
'我有一种感觉,那是因为我再次发布到服务器上了 - - 不,那是因为你*不是*再次发布到服务器。 – GSerg
'location.reload'刷新页面(它会再次调用GET方法,一切都会丢失) –
既然你想重定向到'/ TreatmentOutcomeReport/Report',那么没有必要做一个Ajax调用。只需在POST方法中进行正常的提交和重定向(或者在ModelState无效的情况下返回视图) –