MVC3 ValidationSummary Control

2019-09-16 03:12发布

问题:

I am trying to create validation summary in MVC but I am not getting how to create validation summary. I have created Validation summary in asp.net by using Validation summary control.

I want to display error summary in Bullet list. So, how can I create it in MVC3? What is a better way to create it? Is there any example or demo?

I want to display like this at the top of View.

Please correct the following error

  • First Name required
  • Last Name required

回答1:

use

@Html.ValidationSummary() in .cshtml page.

and Check ModelState in Controller like this

 if (ModelState.IsValid)
            {
                var result = Model.Save();
                if (result)
                {
                    //ViewData["MessageFromServer"] = "<p>Saved Successfully<p>";
                   return  RedirectToAction("Index");

                }
                else
                {
                    ViewData["MessageFromServer"] = "Error while saving";

                }
            }


                return View("Create", Model);

you will see the errors in page.