Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
hi in my project ModelState.AddModelError dosen't show error . of course get error but can't show.

my controller:
C#
[HttpPost]
       public ActionResult Register(tbl_User model)
       {
           if (ModelState.IsValid)
           {
               if (MyClass.IsUserNameDuplicate(model.username) == true)
               {
                   this.ModelState.AddModelError("username", "user name is duplicate");

                   return View(model);
               }
               else
               {
                   MyClass.creatUser(model.username, model.password_User, model.nam);
               }
           }

           return View(model);

       }



my view :
JavaScript
@Html.ValidationSummary(true)
@Html.ValidationMessageFor(model => model.username)
@Html.TextBoxFor(m => m.username, new {@class = "input", @placeholder = "*enter your username" })
Posted
Comments
Mukesh Ghosh 7-Aug-13 6:34am    
If you debug, is it reach in
this.ModelState.AddModelError("username", "user name is duplicate");

also try Html.ValidationSummary("")
Member 8454063 7-Aug-13 9:46am    
i used @Html.ValidationMessage("username") and it worked . but when i have server side error and client side error together , it just shows clientside error . @Html.ValidationMessage("username") alone works , but when i have forexample email required error , it doesn't show .

1 solution

You need to don't specify the key. Correct like this:
C#
ModelState.AddModelError("", "user name is duplicate");


Like this the view
C#
@Html.ValidationSummary(true, "", new { @class = "text-danger" })


display error
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900