0

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
1
  • 2
    have you tried with ViewData.ModelState.AddModelError("", "error") ? Commented Oct 7, 2016 at 15:30

1 Answer 1

2

Yes it is, by adding the errors to the ModelState:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.