Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 722045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:56:37+00:00 2026-05-14T05:56:37+00:00

I am quite confused with how to effectively use the Html.DropDownList helper for ASP.NET

  • 0

I am quite confused with how to effectively use the Html.DropDownList helper for ASP.NET MVC.

Background: 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.

This works for TextBoxes. But not DropDownLists. I have tried a load of different methods, including:

  • How to add static list of items in MVC Html.DropDownList()
  • Setting selected item to DropdownList in MVC Application?

I have a ViewModel such taht I have got my lists and my Model (a LINQ-to-SQL generated class) as properties. eg:

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 => q.Name));
        Currencies = new SelectList(currencies,"CurrencyCode","FriendlyForm");
    }
}

My Controller’s Edit Action on GET looks like:

public class ConsultantRegistrationController : Controller
{
        public IConsultantRegistrationRepository ConsultantRegistrationRepository { get; private set; }
        public ICountryRepository CountryRepository { get; private set; }
    public IEnumerable<ICountry> 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));
    }
}

With my View doing:

        <%: Html.DropDownList("ConsultantRegistration.BankBranchCountry",ViewData["bankBranchCountrySelectList"] as SelectList) %>

This gives me the error:

DataBinding:
‘IWW.ArrowPay.ConsultantRegistration.Data.Country’
does not contain a property with the
name ‘BankBranchCountry’.

Which it does, have a look at the schema of this property:

public interface IConsultantRegistration
{
    Guid ID { get; set; }


    [DisplayName("Branch Country")]
    string BankBranchCountry { get; set; }

}

(My LINQ-to-SQL type ConsultantRegistration implemented IConsultantRegistration)

It seems that it is trying to bind to the wrong type, though?

If I use this in my view (and use my Controller’s Countries property):

        <%: Html.DropDownList("ConsultantRegistration.BankBranchCountry ",Model.Countries,"(select a Country)") %>

I get the saved value fine, but my model doesn’t update on POST.

And if I use this in my view:

        <%: Html.DropDownListFor(model=>model.ConsultantRegistration.BankBranchCountry ",Model.Countries,"(select a Country)") %>

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.

So I have a bit of the solution all over the place, but not all in one place.

Hope you can help fill in my ignorance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:56:37+00:00Added an answer on May 14, 2026 at 5:56 am

    Ok, I solved it. Proper hacky, but it gets the job done.

    I’m using the ViewData in my view:

                    <%: Html.DropDownList("bankBranchCountrySelectList", ViewData["bankBranchCountrySelectList"] as SelectList)%>
    

    With the following in my controller:

            public ActionResult Edit(Guid id, int sectionIndex)
            {
                IConsultantRegistration consultantRegistration = ConsultantRegistrationRepository.GetConsultantRegistration(id);
    
                ViewData["bankBranchCountrySelectList"] = Countries.Select(q => new SelectListItem() { Text = q.Name, Value = q.Name, Selected = (q.Name.Trim().Equals(consultantRegistration.BankBranchCountry, StringComparison.InvariantCultureIgnoreCase)) }); // bankBranchCountriesSelectList;
    
                return View(new ConsultantRegistrationFormViewModel(consultantRegistration,sectionIndex,  Countries,Currencies));
            }
    
    
    
            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult Edit(Guid id, int sectionIndex, FormCollection formValues)
            {
                IConsultantRegistration consultantRegistration = ConsultantRegistrationRepository.GetConsultantRegistration(id); 
    
                UpdateModel(consultantRegistration);
                        ViewData["bankBranchCountrySelectList"] = Countries.Select(q => new SelectListItem() { Text = q.Name, Value = q.Name, Selected = (q.Name.Trim().Equals(consultantRegistration.BankBranchCountry, StringComparison.InvariantCultureIgnoreCase)) });
    
                IEnumerable<RuleViolation> ruleViolations = consultantRegistration.GetRuleViolations(sectionIndex);
                if (ruleViolations.Count() == 0)
                {
    
    // ...
                }
                else
                {
                    ModelState.AddRuleViolations(ruleViolations);
                    return View(new ConsultantRegistrationFormViewModel(consultantRegistration, sectionIndex, Countries, Currencies));
                }
    
    
        }
    

    Not ideal and breaks clean coding. No idea why it works, but that seems to be what MVC is all about with “convention over configuration”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am quite confused on how to have my own layout for each page
I'm quite confused about several books in .NET that I have read. Would someone
I am quite confused. I have wrote an upload form in my site. everything
This is getting me quite confused: I have a small application that uses a
In URL rewriting, i am quite confused that should i use underscore (_) OR
I am quite confused about the type of the object which is rethrown in
I'm quite confused in Razor syntax ))) For example I have an element: <div
I'm quite confused on when to use factor(educ) or factor(agegroup) in R. Is it
Quite confused here. <html> <head> <style> .one { font-weight: normal; } .two { font-weight:
I'm quite confused about the use of methods to manipulate a list in python.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.