views:

35

answers:

1

I've created an HtmlHelper that helps me show a jQuery modal dialog: I set a message in controller's TempData and if the message is not null, the helper writes an jquery + html code to make popup apears after postback. But I need to show the Validation results as a message (same message displayed by ValidationSummary), and I have no Idea how to accomplish this. Can someone help me? Am I doing it the right way?

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()%>[...]
+1  A: 

You could 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);
Alexander Prokofyev
Thanks a lot, it worked like a charm. Now, I just need to figure out how to make it work in client validation side.
Gmoliv