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

New grid modelstate control = new problems

6 Answers 188 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.
Joan Vilariño
Top achievements
Rank 2
Joan Vilariño asked on 04 Oct 2010, 08:33 AM
Hello.

With the last update to the Telerik's extensions for MVC I was happy to see you implemented ModelState support on ajax grids. That's a good update, but it has brought some problems for my application. This is the most important one:
 
I have entities with an Id column. This column is long. The column is used to as main key for the grid, but there's no column for it, as it's not edited. It's generated at INSERT time by the database, so I don't mind it's value when I create a new record.
Ok, since the last update, none of my ajax grids inserts records, because it tells me that the Id column cannot be null... this column, as I stated, is not present at the grid, so I guess you're checking ModelState.IsValid generally instead of checking ModelState for the columns present in the grid, what should be the case... If there are any other errors in the model, I can handle them in my controller, as I do, so while my controller saves the new record, the grid stays in insert mode, and no error is given.

The roundabout I found for this is adding the Id column in the grid, but I don't want it to be visible. If I set it to "Visible(false)" it doesn't even render, so the same error comes up.

The second thing I tried is adding the column with HtmlAttributes(new { style = 'display:none' }) but then, the Id itself doesn't show but the header for the column "Id" is there with a blank column.

My third intent is to make .Width(0) for the colum, but then when I edit the grid, all the columns widths break and the visual for the grid is a mess.

My fourth intent was to place that column the last, then it worked... but only for grids that have .Scrollable property. If you don't set the grid scrollable, then the header appears with a blank column (no ids are showed) as in the second solution I tried.

Please, can you confirm me if this ModelState check is as intented, and what to do when we don't place columns in the grid that can invalidate modelstate? I think I should have the decision on what to validate and when....

If it must be this way (weird), please tell me how to make a column invisible, but posteable so the model don't fail at postback.

Thanks!!!

6 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 04 Oct 2010, 08:44 AM
Hi Joan Vilariño,

 This is strange indeed. Unfortunately I cannot reproduce this behavior in our online demo. We don't include the key there as well but insertion works as expected. Could you please provide a sample project?

In general we indeed check for ModelState.IsValid as we cannot check only columns of the grid (they are not known during ajax editing). If you are need to save your model despite the fact that  ModelState.IsValid returns false you can call ModelState.Clear() before returning from the action method.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joan Vilariño
Top achievements
Rank 2
answered on 04 Oct 2010, 10:23 AM
Hi Atanas, I tried myself to reproduce the problem in a small test project I have, and like you say, it doesn't happen, so it must have something to do with my entities? I don't know... when I have time I'll re-check and trace the problem if I can.

Meanwhile, a ModelState.Clear(); will do for me.

Anyway, I keep having a doubt... now that ModelState is checked in your grid, I understand I can send custom validation problems in it to be showed in the grid... the problem is that if I want to display a row-wide validation message (I mean, not bound to a single column) it won't show, since your grid only shows errors tied to a column in the grid...

Take as example if I want to tell the user the DB is offline or a time-out problem... if I don't tie this to a column it won't show... is there any way to do this?

Thanks!

PS: I keep having problems with non-visible rows, doing weird things, like breaking grid's design, only header appearing and stuff like that...
0
Atanas Korchev
Telerik team
answered on 04 Oct 2010, 10:48 AM
Hello Joan Vilariño,

 I am not sure if it is possible to display model wide errors with inline validation. I mean even without a grid you can't add a model wide error message without the use of validation summary.

I am not sure what you mean by non-visible rows. If you mean hidden columns - there are indeed known issues with them because browsers cannot handle well hidden table columns. I always suggest to use the client-side events instead of relying on hidden columns.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joan Vilariño
Top achievements
Rank 2
answered on 04 Oct 2010, 11:27 AM
Yes .. my fault. I was meaning non visible columns...

For the model-wide message issue, maybe I could suggest you include a new client API event that gets called at post-return time after calling save, like "OnSaveReturn" or anything like that... where I could check the ModelState for my model-wide messages and display them in a separate div or anything and where I could cancel the update with a "return false;" or "e.preventDefault();"

Thank you!
0
Atanas Korchev
Telerik team
answered on 04 Oct 2010, 11:54 AM
Hi Joan Vilariño,

 Thank you for the feedback. Indeed it makes sense to raise an event in case of model state errors. We will consider this for the next release of the grid.

By the way you an use the Bind attribute to make the MVC model binder exclude certain properties:

public ActionResult Insert([Bind(Exclude = "Id")] Customer customer)
{
}

Perhaps this was the problem in your project:

Regards,

Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
El Roy
Top achievements
Rank 1
answered on 19 Nov 2010, 04:59 PM
I also encounter this weird problem.  I notice that when I do Insert/Edit method the grid doesn't go back to normal display, I look at the action method I notice ModelState.IsValid is always set to false after calling TryUpdateModel(). I try to isolate the problem by allowing only to update one property of my model but still the ModelState.IsValid is always set to false.

ModelState.Clear() works like a charm thanks :)
Tags
Grid
Asked by
Joan Vilariño
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
Joan Vilariño
Top achievements
Rank 2
El Roy
Top achievements
Rank 1
Share this question
or