Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When the data is posted, the items listed in the <code>DropDown</code> are not posted back to the model so I am assuming you are not fetching them again and re-adding them to your model before returning your model back to the view.</p> <p>You need to make sure on your post that you are filling <code>Model.StateProvinces</code> and then passing it to your View. Only the values are persisted unlike WebForms which would maintain the <code>DropDownList</code> items in the <code>ViewState</code> and rebuild them for you.</p> <p>So assuming your controller looks something like:</p> <pre><code>// POST: /YourController/YourAction/{ID} [HttpPost] public ActionResult YourAction(YourModel model, int id) { // model.StateProvinceId has the selected value // you need to fill StateProvinces since it is empty as the list is not posted // back so it is not autofilled into your model. model.StateProvinces = LoadYourStateProvincesList(); // This would blow up if StateProvinces is null because your View is assuming that // it has a valid list of StateProvinces in it to build the DropDown return View(model); } </code></pre> <p>I actually asked a question a while back that might be of some use to help explain how to handle DropDown lists:</p> <p><a href="https://stackoverflow.com/questions/2398296/best-way-of-implementing-dropdownlist-in-asp-net-mvc-2">Best way of implementing DropDownList in ASP.NET MVC 2?</a></p> <p><strong>EDIT:</strong> Based on your comment it you are not even getting the first list up... well it looks like you might not have your model setup correctly. Do you even have a model? Your create is just returning <code>View()</code> with no model data in it. You need to create a Model and return it.</p> <p>Eg.</p> <pre><code>public class YourModel { public int StateProvinceId { get; set; } /// ... whatever else you need } // Then in your view your controller you need to set it and create it View(new YourModel() { StateProvinceId = 123 };); </code></pre> <p>You need the model so that when the form IS posted back you can retrieve the value because MVC will stuff the value into your model like th example I posted above regarding the POST part.</p> <p><strong>EDIT:</strong> Ok now that question is clearer and a lot simpler than the original question was making the problem out to be. The problem is your SelectList needs tell it which fields to put where:</p> <pre><code>SelectList theList = new SelectList(stateProvinces, "Value", "Text"); </code></pre> <p>In your original question you had:</p> <pre><code>&lt;%: Html.DropDownListFor(model =&gt; model.StateProvinceId, new SelectList(Model.StateProvinces, "StateProvinceId", "Name") )%&gt; </code></pre> <p>Which was totally correct but it was how you were building up Model.StateProvinces that was wrong so it threw me off thinking it had to be something else. </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