Asp.net

當模型無效時,返回視圖內的局部視圖,並顯示錯誤消息

  • May 16, 2013

在我的 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>
}

控制器:

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

呼叫局部視圖的連結

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

索引頁 索引頁+局部視圖 model.isvalid != true 時返回的部分視圖

從我所見,您正在使用標準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);
   }
}

引用自:https://stackoverflow.com/questions/16588434