Note that there are some explanatory texts on larger screens.

plurals
  1. PODropDownList not working 100% in ASP.Net MVC
    text
    copied!<p>I am quite confused with how to effectively use the Html.DropDownList helper for ASP.NET MVC.</p> <p><strong>Background:</strong> I have a 5-page form, which saves data to the form each time "Next" is clicked. Users may navigate back and forth between sections, so previous sections will already be pre-populated with previously-entered data.</p> <p>This works for TextBoxes. But not DropDownLists. I have tried a load of different methods, including:</p> <ul> <li><a href="https://stackoverflow.com/questions/867117/how-to-add-static-list-of-items-in-mvc-html-dropdownlist/867218#867218">How to add static list of items in MVC Html.DropDownList()</a></li> <li><a href="https://stackoverflow.com/questions/2080802/setting-selected-item-to-dropdownlist-in-mvc-application">Setting selected item to DropdownList in MVC Application?</a></li> </ul> <p>I have a ViewModel such taht I have got my lists and my Model (a LINQ-to-SQL generated class) as properties. eg:</p> <pre><code>public class ConsultantRegistrationFormViewModel { public IConsultantRegistration ConsultantRegistration { get; private set; } public SelectList Titles { get; private set; } public SelectList Countries { get; private set; } public SelectList Currencies { get; private set; } public int CurrentSection { get; private set; } private ConsultantRegistrationFormViewModel(IConsultantRegistration consultantRegistration) { ConsultantRegistration = consultantRegistration; CurrentSection = 1; Titles = new SelectList(new string[] { "Mr", "Mrs", "Miss", "Ms", "Dr", "Sir" }); Countries = new SelectList(countries.Select(q =&gt; q.Name)); Currencies = new SelectList(currencies,"CurrencyCode","FriendlyForm"); } } </code></pre> <p>My Controller's Edit Action on GET looks like:</p> <pre><code>public class ConsultantRegistrationController : Controller { public IConsultantRegistrationRepository ConsultantRegistrationRepository { get; private set; } public ICountryRepository CountryRepository { get; private set; } public IEnumerable&lt;ICountry&gt; Countries { get; private set; } public ConsultantRegistrationController() { ConsultantRegistrationRepository = RepositoryFactory.CreateConsultantRegistrationRepository(); CountryRepository = RepositoryFactory.CreateCountryRepository(); Countries = CountryRepository.GetCountries().ToArray(); } public ActionResult Edit(Guid id, int sectionIndex) { IConsultantRegistration consultantRegistration = ConsultantRegistrationRepository.GetConsultantRegistration(id); SelectList bankBranchCountriesSelectList = new SelectList(Countries, "BankBranchCountry", "CountryName", consultantRegistration.BankBranchCountry); ViewData["bankBranchCountrySelectList"] = bankBranchCountriesSelectList; return View(new ConsultantRegistrationFormViewModel(consultantRegistration,sectionIndex, Countries,Currencies)); } } </code></pre> <p>With my View doing:</p> <pre><code> &lt;%: Html.DropDownList("ConsultantRegistration.BankBranchCountry",ViewData["bankBranchCountrySelectList"] as SelectList) %&gt; </code></pre> <p>This gives me the error:</p> <blockquote> <p>DataBinding: 'IWW.ArrowPay.ConsultantRegistration.Data.Country' does not contain a property with the name 'BankBranchCountry'.</p> </blockquote> <p>Which it does, have a look at the schema of this property:</p> <pre><code>public interface IConsultantRegistration { Guid ID { get; set; } [DisplayName("Branch Country")] string BankBranchCountry { get; set; } } </code></pre> <p>(My LINQ-to-SQL type ConsultantRegistration implemented IConsultantRegistration)</p> <p>It seems that it is trying to bind to the wrong type, though?</p> <p>If I use this in my view (and use my Controller's Countries property):</p> <pre><code> &lt;%: Html.DropDownList("ConsultantRegistration.BankBranchCountry ",Model.Countries,"(select a Country)") %&gt; </code></pre> <p>I get the saved value fine, but my model doesn't update on POST.</p> <p>And if I use this in my view:</p> <pre><code> &lt;%: Html.DropDownListFor(model=&gt;model.ConsultantRegistration.BankBranchCountry ",Model.Countries,"(select a Country)") %&gt; </code></pre> <p>I get the list, and it POSTs the selected value back, but does not pre-select the currently selected item in my model on the view.</p> <p>So I have a bit of the solution all over the place, but not all in one place.</p> <p>Hope you can help fill in my ignorance.</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