I have some code that renders Partial View based on some model to html.
And after this I send this html to the page.
In case of the errors I would like to use
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
in order to display them.
So my question is following: Is it posible to add some errors to model right before the rendering?
#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