As I am working thru the NerdDinner MVC example. I found that after implementing step 6 that edits to a dinner were no longer saved. After some investigation I found that I needed to add the ValueProvider to the UpdateModel function call in Edit (HttpPost)
example. My updated code is below.
The 'TryUpdateModel' method you used need 3 parameters. The third one is an object which implement the IValualProvider interface.
So the provider you mentioned is necessary for the code.
Maybe the better solution is to use the default model binder.
e.g.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Movie movieToEdit)
{
var originalMovie = (from m in _db.MovieSet
where m.ID == movieToEdit.ID
select m).First();
if (!ModelState.IsValid)
return View(originalMovie);
_db.ApplyPropertyChanges(originalMovie.EntityKey.EntitySetName, movieToEdit);
_db.SaveChanges();
return RedirectToAction("Index");
}
PS: The sample above use ADO.NET Entity Data Model
I guess I should be more explicit. The NerdDinner code uses updateModel with a single parameter. That does not seem to work. So is my solution to the issue correct or is there some better way to do it. What changes have occured in the latest iteration of
MVC to require that code change?
NerdDinner is getting a massive update, according to Scott Hanselman's blog,
Mix 10 Rollup Post:
Jon Galloway and I take
NerdDinner and add OpenAuth via Twitter, OpenID, OpenSearch, OData, iCal, microformats, RSS, social sharing, embeddable blog flair, tinyurls and geolocation. The code is checked in (and still being worked on) at
http://nerddinner.codeplex.com and the app is always at
http://www.nerddinner.com.
The blog post also has a link to video of their session:
WMV, WMVHigh,
MP4
I'm afraid all this might make NerdDinner top heavy, just to show off all those new buzzwords. But probably worth a look anyway.
Don
Don Kiely, MCP, MCSD
In the Last Frontier, Interior Alaska
Please post questions and replies to the forum! And remember to MARK AS ANSWER when someone definitively answers a question or resolves a problem!
Mark_KDL
Member
2 Points
15 Posts
Change needed to NerdDinner Mvc example?
Mar 15, 2010 07:45 PM|LINK
As I am working thru the NerdDinner MVC example. I found that after implementing step 6 that edits to a dinner were no longer saved. After some investigation I found that I needed to add the ValueProvider to the UpdateModel function call in Edit (HttpPost) example. My updated code is below.
Questions:
1. Was this code change really necessary?
2. Is there a better way to do it?
Modified Code:
// // POST: /Dinners/Edit/5 [HttpPost] public ActionResult Edit(int id, FormCollection collection) { Dinner dinner = _repositoryContext.GetDinner(id); try { IValueProvider provider = this.ControllerContext.Controller.ValueProvider; UpdateModel( if (TryUpdateModel(dinner, "Dinner", provider )) { _repositoryContext.Save(); return RedirectToAction("Details", new { id = dinner.DinnerID }); } else { ModelState.AddRuleViolations(dinner.GetRuleViolations()); return View(new DinnerFormViewModel(dinner)); } } catch { return View("Error"); } }UpdateModel NerdDinner mvc - 2
KeFang Chen ...
Star
8329 Points
852 Posts
Re: Change needed to NerdDinner Mvc example?
Mar 17, 2010 07:19 AM|LINK
Hi,
The 'TryUpdateModel' method you used need 3 parameters. The third one is an object which implement the IValualProvider interface.
So the provider you mentioned is necessary for the code.
Maybe the better solution is to use the default model binder.
e.g.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Movie movieToEdit) { var originalMovie = (from m in _db.MovieSet where m.ID == movieToEdit.ID select m).First(); if (!ModelState.IsValid) return View(originalMovie); _db.ApplyPropertyChanges(originalMovie.EntityKey.EntitySetName, movieToEdit); _db.SaveChanges(); return RedirectToAction("Index"); }PS: The sample above use ADO.NET Entity Data Model
Mark_KDL
Member
2 Points
15 Posts
Re: Change needed to NerdDinner Mvc example?
Mar 17, 2010 03:25 PM|LINK
I guess I should be more explicit. The NerdDinner code uses updateModel with a single parameter. That does not seem to work. So is my solution to the issue correct or is there some better way to do it. What changes have occured in the latest iteration of MVC to require that code change?
KeFang Chen ...
Star
8329 Points
852 Posts
Re: Change needed to NerdDinner Mvc example?
Mar 18, 2010 02:00 AM|LINK
Hi,
Maybe the following thread is helpful for you to understand the method.
http://forums.asp.net/p/1357154/2789000.aspx
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Change needed to NerdDinner Mvc example?
Mar 18, 2010 06:06 AM|LINK
ND is really out of date. I suggest you start working with MVC 2 samples. See the MVC FAQ and http://code.msdn.microsoft.com/aspnetmvcsamples
donkiely
All-Star
15929 Points
2518 Posts
ASPInsiders
Moderator
MVP
Re: Change needed to NerdDinner Mvc example?
Mar 18, 2010 06:19 AM|LINK
NerdDinner is getting a massive update, according to Scott Hanselman's blog, Mix 10 Rollup Post:
Jon Galloway and I take NerdDinner and add OpenAuth via Twitter, OpenID, OpenSearch, OData, iCal, microformats, RSS, social sharing, embeddable blog flair, tinyurls and geolocation. The code is checked in (and still being worked on) at http://nerddinner.codeplex.com and the app is always at http://www.nerddinner.com.
The blog post also has a link to video of their session: WMV, WMVHigh, MP4
I'm afraid all this might make NerdDinner top heavy, just to show off all those new buzzwords. But probably worth a look anyway.
Don
In the Last Frontier, Interior Alaska
Please post questions and replies to the forum! And remember to MARK AS ANSWER when someone definitively answers a question or resolves a problem!