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.
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.
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
user).
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.
Reporting by definition is different. Otherwise we would just show it on the screen.
n8900498
Member
47 Points
69 Posts
Submitting a Model to a Controller via HttpPost turns some of my Model fields to NULL
Dec 12, 2012 03:35 PM|LINK
Hi
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) </div>Any help will be appreciated
jpriestley70
Member
80 Points
15 Posts
Re: Submitting a Model to a Controller via HttpPost turns some of my Model fields to NULL
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)
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: Submitting a Model to a Controller via HttpPost turns some of my Model fields to NULL
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.
eric2820
Contributor
2777 Points
1161 Posts
Re: Submitting a Model to a Controller via HttpPost turns some of my Model fields to NULL
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 user).
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ryanbesko
Contributor
3551 Points
619 Posts
Re: Submitting a Model to a Controller via HttpPost turns some of my Model fields to NULL
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.
eric2820
Contributor
2777 Points
1161 Posts
Re: Submitting a Model to a Controller via HttpPost turns some of my Model fields to NULL
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?!?
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.