0

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
tereško
  • 58,060
  • 25
  • 98
  • 150
Ajay
  • 6,418
  • 18
  • 79
  • 130
  • See this post: http://stackoverflow.com/questions/2818219/asp-net-mvc-html-validationsummarytrue-does-not-display-model-errors – Curtis Nov 29 '13 at 10:25
  • @Curt I want to display the error list at the top of page in bullet list format. – Ajay Nov 29 '13 at 10:27

1 Answers1

2

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.

Priyank
  • 1,353
  • 9
  • 13
  • Thank you. It works. No need to add ViewData. I added only `@Html.ValidationSummary()` – Ajay Nov 29 '13 at 15:37
  • use place holder attribute. it dose not work on old versions of ie. refer link http://www.w3schools.com/tags/att_input_placeholder.asp – Priyank Dec 02 '13 at 05:44
  • I tried it. It works in TextBoxFor control but not in EditorFor control. – Ajay Dec 02 '13 at 06:14