8

In my asp.net MVC 4 project, I like to safe something from a partial view, which is when a user clicks for "more details". Saving the data is no problem, closing the partial view is no problem, open the partial view is not a problem, it's when my model is not valid (when a user forgets to mark something) The result is that my partial view is returned, but not inside the view where it should be. Its just viewed as a standalone page.

View:

@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>
}

Controller:

[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);
    }

The link that calls the partial view

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

Index Page Index page + partial view Partial view returned when model.isvalid != true

4

1 回答 1

11

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

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

@using (Ajax.BeginForm("ChangeEvaluator", "Ontwikkelplan", new AjaxOptions { OnSuccess = "handleSuccess" }))
{
    ...
}

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

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

最后编写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);
    }
}
于 2013-05-16T13:22:08.567 回答