Note that there are some explanatory texts on larger screens.

plurals
  1. POModel binding not working with labelfor
    text
    copied!<p>Here is my model:</p> <pre><code> public string CustomerNumber { get; set; } public string ShipMethod { get; set; } public string ContactPerson { get; set; } public string ShipToName { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string Address3{ get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } </code></pre> <p>Here part of my view:</p> <pre><code> &lt;table class="table table-condensed"&gt; &lt;thead&gt; &lt;tr&gt; &lt;td&gt;Customer Number&lt;/td&gt; &lt;td&gt;Ship Method&lt;/td&gt; &lt;td&gt;Contact Person&lt;/td&gt; &lt;td&gt;Ship to Name&lt;/td&gt; &lt;td&gt;Address 1&lt;/td&gt; &lt;td&gt;Address 2&lt;/td&gt; &lt;td&gt;Address 3&lt;/td&gt; &lt;td&gt;City&lt;/td&gt; &lt;td&gt;State&lt;/td&gt; &lt;td&gt;Zip&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.CustomerNumber, Model.CustomerNumber)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.ShipMethod, Model.ShipMethod)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.ContactPerson, Model.ContactPerson)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.ShipToName, Model.ShipToName)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.Address1, Model.Address1)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.Address2, Model.Address2)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.Address3, Model.Address3)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.City, Model.City)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.State, Model.State)&lt;/td&gt; &lt;td&gt;@Html.LabelFor(x =&gt; x.ZipCode, Model.ZipCode)&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>The data gets displayed in the view correctly, but when a post happens on my page, the data comes back null. I need the data in those LabelFors to be sent back to my controller, so I dont have to store it in a session. I thought MVC was supposed to bind to your model automagically with the labelfors. What am I doing wrong?</p> <p><strong>EDIT</strong> </p> <p>I guess the reason I asked this question, is because I just moved from webforms to MVC, and I am pretty lost without the viewstate. I figured if the values in my model kept posting back to me from the view, I wouldn't have to store my model in a session object. On my page, I need to be able to persist my model during page cycles, so I can store my model data into some sql tables after the user clicks the save button. What are some options to persist your model in MVC?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload