Continuing from this question, I've got a form with all the Vehicles listed for a Person, the vehicle fields are editable, and they are now successfully posting back to my Save action.
Now I'd like to use UpdateModel to save the data, but I'm not sure how to construct it. Here's my Save action right now:
<ActionName("Edit"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As Person, ByVal vehicles() As Vehicle) As ActionResult
Dim original = From v In MyDataContext.Vehicles Where v.person_id = Person.person_id
For Each item In original
For i = 0 To vehicles.Count - 1
If vehicles(i).vehicle_id = item.vehicle_id Then
UpdateModel(item, New String() {"license_nbr", "color"})
Exit For
End If
Next
Next
MyDataContext.SubmitChanges()
Return RedirectToAction("Index", "Home")
End Function
When I run this, it doesn't save anything, and UpdateModel doesn't throw any errors. I'm assuming I have to give it a little more direction to get the magic to work, because UpdateModel doesn't know which item in the vehicles array to use for each update.
Do I need to specify a ValueProviderResult as the third parameter to UpdateModel? If so, how do I create one out of vehicles(i)? Am I completely off base in how I have this set up?
-
Why use UpdateModel -- which just updates the properties from form fields -- when you already have the form fields processed into model data? Can't you just assign the values from
vehicle
toitem
directly?For Each item In original For i = 0 To vehicles.Count - 1 If vehicles(i).vehicle_id = item.vehicle_id Then item.license_nbr = vehicles(i).license_nbr item.color = vehicles(i).color Exit For End If Next Next
gfrizzle : I could definitely do that, but can't you make that argument against UpdateModel across the board? Not arguing for or against, just learning.tvanfosson : Normally I would use UpdateModel when not using model binding, i.e., there are no parameters or only an id parameter to the method and I get the values via UpdateModel from the ValueProvider directly. In the case where you already have a bound model I don't think it makes sense to use UpdateModel.
0 comments:
Post a Comment