This is a migrated thread and some comments may be shown as answers.

[Solved] Grid - ModelState jscript error

1 Answer 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Marcin
Top achievements
Rank 1
Marcin asked on 19 Jul 2011, 12:10 PM
i found two similar topics
http://www.telerik.com/community/forums/aspnet-mvc/grid/control-delete-edit-errors-in-ajax-mode.aspx
http://www.telerik.com/community/forums/aspnet-mvc/grid/grid-client-side-custom-validation.aspx

from one year ago. My problem is that for example i want verify if login and email for new users are unique so..
public ActionResult _AjaxCreateUser(UserModel model)
       {
           if (ModelState.IsValid)
           {
               if(userService.CheckLoginUnique(model.UserLogin))
               {
                   ModelState.AddModelError("", I18n.USER_LOGIN_NOT_UNIQUE);
               }
               else 
                 if(userService.CheckEmailUnique(model.UserEmail))
                 {
                     ModelState.AddModelError("", I18n.USER_EMAIL_NOT_UNIQUE);
                 }
                 else
                   if (TryUpdateModel(model))
                   {
                       UsersDTO usersDTO = new UsersDTO();
                       UserItem newUser = GetUserItemFromUserModel(model);
                       usersDTO.UserItems.Add(newUser);
                       User userAdded = userService.CreateUsers(usersDTO);
                       Token newToken = new Token();
                       newToken.User = userAdded;
                       newToken.TokenGuid = TokenUtil.GenerateToken();
                       tokenService.SaveToken(newToken);
                       mailService.SendEmailForNewUser(model.UserEmail, newToken);
                   }
           }
           
           return View(new GridModel(GetAllUsers()));
       }

But if i add my custom error to ModelState i got some strange jscript error and nothing is do - popup doesnt dissapear and i dont see my custom error. If you create support for this function ?

1 Answer, 1 is accepted

Sort by
0
Tom
Top achievements
Rank 1
answered on 14 Feb 2012, 10:49 AM
I just ran in to this too.  It turns out the GridModel will send back ModelState errors to the client.  This is a good thing.

But, a problem occurs if you don't add a key name to the ModelError.  IE
 
ModelState.AddModelError("", "No way will that work.  Try again.");

Instead of
ModelState.AddModelError("Email", "No way will that work.  Try again.");

It turns out that the client side javascript attempts to find the right control to display the error at using "#<key>" .  With a blank key it tries to find the control with jQuery('#') which fails.

Two ways you could work around this. 
1)  Add a key.  or
2) Change your server side code to change the Response Status Code when ModelState errors contain a blank key.  An ActionFilter would work well for this.  Append the errorMessage to the Response header, Then handle the OnError client side event and alert the user.

Tags
Grid
Asked by
Marcin
Top achievements
Rank 1
Answers by
Tom
Top achievements
Rank 1
Share this question
or