当model无效时,返回视图内的部分视图,并显示错误消息

Dyl*_*nck 8 asp.net asp.net-mvc asp.net-ajax partial-views asp.net-mvc-4

在我的asp.net MVC 4项目中,我喜欢从局部视图中保护某些内容,即当用户点击"更多详细信息"时.保存数据没问题,关闭局部视图没问题,打开局部视图不是问题,当我的模型无效时(当用户忘记标记某事时)结果是返回了我的局部视图,但不应该在视图里面.它只被视为独立页面.

视图:

@model Evaluatietool.ViewModels.EvaluatorWijzigenOPViewModel
<h3>@ViewBag.Message</h3>
@using (Html.BeginForm("ChangeEvaluator", "Ontwikkelplan"))
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.oldEvalAccount)
    @Html.HiddenFor(model => model.selectedMedewerkerAccount)
    @Html.HiddenFor(model => model.eval);
    @Html.HiddenFor(model => model.countMedewerkers);
...

...
<div class="Buttons">
    <input type="submit" value="Submit" />
    @Ajax.ActionLink("Sluiten", "Evaluatorenlijst", new AjaxOptions { OnSuccess = "HideResultDiv" })
</div>
}
Run Code Online (Sandbox Code Playgroud)

控制器:

[HttpPost]
    public ActionResult ChangeEvaluator(EvaluatorWijzigenOPViewModel ewopvm)
    {
        if (ModelState.IsValid)
        {
            if (ewopvm.selectedObjects != null)
            {
                ewopvm.selectedObjects.Add(ewopvm.selectedMedewerkerAccount);
            }
            else
            {
                ewopvm.selectedObjects = new List<string>();
                ewopvm.selectedObjects.Add(ewopvm.selectedMedewerkerAccount);
            }
            Ontwikkelplannen op = new Ontwikkelplannen();
            Evaluaties e = new Evaluaties();
            foreach (string s in ewopvm.selectedObjects)
            {
                op.ChangeEvaluator(ewopvm.newEvalAccount, ewopvm.oldEvalAccount, s, ewopvm.eval);
            }
            return RedirectToAction("Evaluatorenlijst");
        }
        return PartialView("EvaluatorWijzigenPartial", ewopvm);
    }
Run Code Online (Sandbox Code Playgroud)

调用局部视图的链接

 @Ajax.ActionLink(item.Evaluator1.Naam, "EvaluatorWijzigenPartial", new { id = item.ID,     eval = true }, new AjaxOptions { UpdateTargetId = "EvaluatorWijzigen", OnComplete = "ShowResultDiv"})
Run Code Online (Sandbox Code Playgroud)

索引页面 索引页面+局部视图 当model.isvalid!= true时返回部分视图

Dar*_*rov 11

从我可以看到,您正在使用标准Html.BeginFormPOST到ChangeEvaluator控制器操作,该操作执行重定向或在验证失败时返回部分视图.

所以你观察到的行为是完全正常的.如果要实现这一目的,您必须使用AJAX提交此表单:

@using (Ajax.BeginForm("ChangeEvaluator", "Ontwikkelplan", new AjaxOptions { OnSuccess = "handleSuccess" }))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后你可以调整你的控制器动作,以便在成功的情况下它不重定向,但它返回一个包含要重定向到的url的Json对象:

[HttpPost]
public ActionResult ChangeEvaluator(EvaluatorWijzigenOPViewModel ewopvm)
{
    if (ModelState.IsValid)
    {
        ...
        return Json(new { redirectTo = Url.Action("Evaluatorenlijst") });
    }
    return PartialView("EvaluatorWijzigenPartial", ewopvm);
}
Run Code Online (Sandbox Code Playgroud)

最后写了handleSuccessjavascript函数:

function handleSuccess(result) {
    if (result.redirectTo) {
        // The controller action returned a JSON object with the redirectTo property
        // let's redirect to this location:
        window.location.href = result.redirectTo;
    } else {
        // The controller action returned a partial view with the form and the errors
        // So we need to update some containing DIV with it:
        $('#someDivThatCOntainsYourForm').html(result);
    }
}
Run Code Online (Sandbox Code Playgroud)