How to receive ValidationSummary program messages?

I created an HtmlHelper that helps me show the jQuery modal dialog: I set the message in the TempData controller, and if the message is not null, the helper writes jquery + html code so that the tooltips after the postback. But I need to show the test results as a message (the same message displayed by ValidationSummary), and I have no idea how to do this. Can anybody help me? Am I doing it right?

My helper.cs:

[...] 
        public static string ModalDialogNotifier(this HtmlHelper helper)
        {
            string message = "";
            if (helper.ViewContext.TempData["message"] != null)
                message = helper.ViewContext.TempData["message"].ToString();
        if (!String.IsNullOrEmpty(message))
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script>$(document).ready(function() {$.blockUI({ message: $('#mdiag')});$('#mdiagok').click(function() {$.unblockUI();return false;});})</script>");
            sb.AppendFormat("<div id='mdiag'>{0}<input type='button' id='mdiagok' value='Ok' /></div>", message);
            return sb.ToString();
        }
        return string.Empty;
    }
[...]

My controller:

 [HttpPost]
    [Authorize(Roles = "Admin")]
    public ActionResult Create(CreateUserModel Model)
    {
        if (!ModelState.IsValid)
        {
            this.TempData["message"] = "Model is not valid";
        }
        else
        {
           [...]
        }
        return View(Model);
    }

My view:

 [...]<%= Html.ModalDialogNotifier()%>[...]
+3
source share
1 answer

You can do something like this:

StringBuilder sb = new StringBuilder();

foreach (ModelState state in ModelState.Values)
    foreach (ModelError error in state.Errors)
        sb.AppendFormat("<div>{0}</div>", error.ErrorMessage);
+3
source

Source: https://habr.com/ru/post/1770193/


All Articles