0

レンダリングするコードがありますパーシャルビュー一部のモデルをhtmlに基づいています。レンダリング前にパーシャルビューにエラーを追加する

この後、私はこのhtmlをページに送ります。私はそれらを表示するために

@Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

を使用したいエラーの場合は

私の質問は次のとおりです:レンダリングの直前にモデルにいくつかのエラーを追加するのは可能でしょうか?

#region Regenerate Partial View in case of error 
var moduleLocation = new ModuleLocation(); // Some custom class 

string renderedPartialView = RenderPartialViewToString("_CreateLocationModalPartial", moduleLocation); 
#endregion 



#region Method to render Partial View 
public string RenderPartialViewToString(string viewName, object model) 
{ 
      if (string.IsNullOrEmpty(viewName)) 
       viewName = ControllerContext.RouteData.GetRequiredString("action"); 

      ViewData.Model = model; 

      using (StringWriter sw = new StringWriter()) 
      { 
       ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); 
       ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); 
       viewResult.View.Render(viewContext, sw); 

       return sw.GetStringBuilder().ToString(); 
      } 
} 
#endregion 
+2

あなたは( ""、 "エラー")ViewData.ModelState.AddModelErrorで試してみましたか? –

答えて

2

はい、それはModelStateにエラーを追加することにより、次のとおりです。

ViewData.ModelState.AddModelError("key", "error")

関連する問題