Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript location.reload删除模型状态错误_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

Javascript location.reload删除模型状态错误

Javascript location.reload删除模型状态错误,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我的控制器中有以下操作: [HttpPost] public JsonResult RedirectToAspReportViewer(MvcReportPeriodSelectionViewModel periodFilter, MvcMultipleLocationSelectionViewModel locationFilter) { var jsonObject = new { HasErrors = false, strUrl = "/ASPReport

我的控制器中有以下操作:

[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中,我有以下函数,get在我的ajax帖子的OnSuccess函数中被调用

onSuccessCall: function (response) {
    if (response.HasErrors === true) {
        //window.location = window.location.href;
        location.reload();
    } else {
        window.open(response.strUrl);
    }
};
从上面可以看到,如果我的response对象有错误,我希望停留在当前页面上,只是刷新它,这样我的ModelState错误仍然会显示出来

我面临的问题是,当我调用
location.reload
时,我的ModelState错误不会显示在我的页面上。我有一种感觉,这是因为我再次发布到服务器,并且ModelState被清除

我怎样才能避免这种情况

更新 我不能只是将验证错误添加到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">&times;   </button>
        @Html.ValidationSummary(false, message)
    </div>
}
@model ModelStateDictionary
@{ 
var cls=“警报危险”;
var type=ViewBag.type;
if(type!=null&&type.ToString().ToLower()=“警告”)
{
cls=“警报警告”;
}
var message=“请注意以下问题:”;
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))
{
&时代;
@Html.ValidationSummary(false,消息)
}
这将在我所有视图的顶部调用,如下所示:

<div id="valSummary">
    @Html.Partial("_ValidationSummaryDisplay", ViewData.ModelState)
</div>

@Html.Partial(“_ValidationSummaryDisplay”,ViewData.ModelState)

如果希望ModelState错误显示在页面上,则应

return View(yourViewModel);
对视图进行编码时,请确保包含用于显示验证的帮助程序:

@Html.ValidationMessage(m => m.PropertyName)

我假设你的handle方法将错误置于ModelState(这就是它的作用)。

我感觉这是因为我再次向服务器发布了
-不,那是因为你没有再次向服务器发布。
location.reload
刷新页面(它再次调用你的GET方法,一切都丢失了)由于您希望重定向到
/treatmentOutComerReport/Report
,因此没有必要进行ajax调用。只需在POST方法中执行正常的提交和重定向(如果
ModelState
无效,则返回视图)@StephenMuecke所有操作都按照建议进行,但问题是,我尝试加载的报表位于一个aspx页面中,我想在一个新选项卡中打开该页面。由于target=“_blank”位于razor视图中,因此如果显示验证错误,它将显示在打开的新选项卡中,而不是原始选项卡中。我尝试了这种方法来处理报告中的新选项卡打开,或者使用jqueryUse客户端验证停留在当前选项卡上并显示错误?返回一个没有或没有
窗口的视图。位置=“…”
取决于验证结果?我知道我可以返回视图,如果在这种情况下我可以这样做,我就不会有这个问题。如何使用jquery获得与return视图相同的行为?好的,那么您需要在JON结果中返回错误,您的java脚本将负责呈现它。重新加载页面会使您丢失验证信息。