So I'm doing this microsoft tutorial on ASP.NET Core with EF 6 and it just went through updating a model through the edit-controller.
There is this piece of code that have me really confused that I imagine (and perhaps hope) isn't as confusing to a lot of you.
var studentToUpdate = await _context.Students.SingleOrDefaultAsync(s => s.ID == id);
if (await TryUpdateModelAsync<Student>(
studentToUpdate,
"",
s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
// goes on to save the context
So, the only thing this controller takes as parameter is the int id
and that's how it gets a hold of the studentToUpdate
. What I'm not entirely familiar with here, is where does it get the update-values from?
What I know:
TryUpdateModelAsync<Student>
studentToUpdate.FirstMidNames
was Carson
(original) but after the function had been executed it was Carsey
(new). The string Carsey
was always in the this>Request>Form>Results View (which held a list of all the values from the form).So I understand that the TryUpdateModelAsync
function somehow uses the linq-statement and the form-result to get the new values for studentToUpdate
, but I really don't see how and where it does this?
Without going into too much technical details. The call to TryUpdateModelAsync
in the provided example
if (await TryUpdateModelAsync<Student>(
studentToUpdate,
"",
s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)){
//...
}
Updates the specified Student
instance studentToUpdate
using values from the controller's current ControllerContext
, which would have been populated with data provided in the request. It uses lambda expression(s) which represent top-level properties which need to be included for the current model when trying the update. It will take only the values of those properties and update the model.
So in the above example even if the entire model was provided by the form it will only update the FirstMidName
, LastName
, and EnrollmentDate
on the specified instance.
I think the most confusing thing about this is Third argument: a linq-statement.
Every linQ expression [i => i.property
] will match with each column inside table or the constraint (Foreign Key, Index,...) (what we know as navigation properties in EF MVC).
So, Third argument decides what should be updated (async) to the Datebase. And it does this via model binding Link to model binding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With