MVC3 ValidationSummary Control

只愿长相守 提交于 2019-12-11 19:34:18

问题


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.



来源:https://stackoverflow.com/questions/20283480/mvc3-validationsummary-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!