Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following line of code is wrong:</p> <pre><code>new SelectList(cities.AsEnumerable(), "CityID", "Name") </code></pre> <p>You are specifying <code>CityID</code> and <code>Name</code> properties but <code>cities</code> is an <code>IEnumerable&lt;SelectListItem&gt;</code>. And <code>SelectListItem</code> only has <code>Value</code> and <code>Text</code> properties. There's no such thing as <code>CityID</code> and <code>Name</code>.</p> <p>So you probably meant:</p> <pre><code>ViewData["AllCities"] = cities != null ? new SelectList(cities, "Value", "Text") : new SelectList(DefaultCities, "Name"); </code></pre> <p>or simply:</p> <pre><code>ViewData["AllCities"] = cities != null ? cities : new SelectList(DefaultCities, "Name"); </code></pre> <p>and in your view:</p> <pre><code>@Html.DropDownList( "CityID", ViewData["AllCities"] as IEnumerable&lt;SelectListItem&gt;, "--Choose One--" ) </code></pre> <p>As far as the <code>CityID</code> argument in your view is concerned, this will basically be used by the DropDownList helper to generate the <code>name</code> property of the select element in your DOM and as a consequence that will be the selected value name being passed to the controller action that you are submitting your form to.</p> <p>By the way using ViewData/ViewBag is very bad practice. I would recommend you using a view model instead and have a strongly typed DropDownListFor helper in your view which will be tied to a specific property on your view model.</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