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

TryUpdateModel - the model of type could not be updated

4 Answers 67 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.
Mark Shortt
Top achievements
Rank 1
Mark Shortt asked on 23 May 2011, 08:08 AM

Hi

I'm using Telerik's MVC Grid to edit some records in MVC3, using Razor view.

I call the edit on the controller using the following code:

public ActionResult _CategoriesUpdate(int id)
   
{
        WR_TakeAway_Menu_Categories category
= db.WR_TakeAway_Menu_Categories.Where(c => c.ID == id).Single();
         
TryUpdateModel(category);            
        db
.ApplyCurrentValues(category.EntityKey.EntitySetName, category);

        db
.ObjectStateManager.ChangeObjectState(category, EntityState.Modified);
        db
.SaveChanges();

Although this updates the records in the serer, it keeps the grid in edit mode because it was unable to update all the properties of the "category".


If I change TryUpdateModel to UpdateModel it throws an error saying "the model of type WR_TakeAway_Menu_Categories could not be updated", but all the changes are populated on the object.


Is there a better way of doing this, or some way to allow TryUpdateModel to return true to allow the grid to return to display mode?

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 23 May 2011, 08:28 AM
Hi Mark Shortt,

One possible workaround is to clear the model state errors or manually update the model from the posted values. 

Does TryUpdateModel work if used outside of the grid (in a Html.Form)?

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
Mark Shortt
Top achievements
Rank 1
answered on 23 May 2011, 08:30 AM
Thanks for you quick reply. I haven't tried it yet with a from, but will do now.

Can you give examples of the other methods you suggest? I'm fairly new to MVC

Thanks

Mark
0
Accepted
Atanas Korchev
Telerik team
answered on 23 May 2011, 09:21 AM
Hi Mark Shortt,

 You can add an FormCollection argument to your action method:

public ActionResult _CategoriesUpdate(int id, FormCollection form)
{
   var category = GetCategoryById(id);
   category.Name = form["Name"];
}
This stackoverflow question may also be of help.

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
Mark Shortt
Top achievements
Rank 1
answered on 23 May 2011, 10:52 AM
Thanks

Manually updating from the form values worked
Tags
Grid
Asked by
Mark Shortt
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Mark Shortt
Top achievements
Rank 1
Share this question
or