Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 12, 2012 11:08 PM by eric2820
Dec 12, 2012 03:35 PM|LINK
I am really new to MVC, I have been doing ASP.NET for many years
I need some help in understanding why when I submit my Model to the Controller via a HttpPost that any field of the Model that I have NOT directly use it NULLs that field. I was hoping that the Model that I send to the view which has values in it I will
get back on a HttpPost but the only fields that are not NULL are the fields that I directly use in the form.
So what I had to do is this (shown below) just so that I can my Idea object back in the Controller with all the field values still intact.
<div style="display: none;">
@Html.EditorFor(m => Model.Idea)
Any help will be appreciated
Dec 12, 2012 03:43 PM|LINK
You need to use @Html.HiddenFor() to reference fields you are not using.
Example: @Html.HiddenFor(m => Model.Field)
Dec 12, 2012 03:54 PM|LINK
the model is not sent to the browser, nor does the browser send the model to the controller. the view produces html from the model. MVC postback model binding allows mapping html form fields (input, select, textarea sent as name.value pairs) to controller
action parameters by their name. if one of the parameters just happens to be the same class as the model sent to the view, and the binding name match, then the model will be filled in with the postback values. as you discovered, only properties with a posted
form fiedl are filled in (where else woudl the value come from).
a common mvc pattern, is to store the model in server persistant store, then rather than bind to a parameter, use TryUpdateModel to apply the subset of postback values to the orginal model values. this also allows detecting of modiifed values.
Dec 12, 2012 08:26 PM|LINK
I tried using TryUpdateModel with one of my record update methods and guess what? It did not work!
What I went back to you can see at: www.my-msi.net which points not to the site, but to a google blog site.
Basically what I did was create a Copy( type data ) method on each of the two classes that get updated.
Then when the data comes back to the controller, I get the record again from the database and copy the user record to that. This method workes but only if every field is popultated (or in my case if I poplulate the one field that isn't poplultated by the
Dec 12, 2012 10:02 PM|LINK
Do you have every field you expect to have access to the value of on your view using @Html.EditorFor or @Html.HiddenFor? If not, no values were sent to the page so they aren't there for your controller action to bind to when the page is posted.
You either need to include each field you expect to have the value for when the page is posted or fetch the record from the database again, update the fields in the fetched record with fields from the posted one, and save the fetched record.
Dec 12, 2012 11:08 PM|LINK
Yes, all three fields in the model are present in the View!
How else would I notice that they wern't being poplulated?!?