0

I need to bind a variable number of dropdownlists, how do I achieve this?

In my project, until now I have only had to deal with binding to a known number of dropdown lists. To do this I have simply done the below:

viewModel

//for staff ddl
public List<Staff> Staff { get; set;     
public int? SelectedStaffId { get; set; }
public IEnumerable<System.Web.Mvc.SelectListItem> StaffItems
{
    get { return new System.Web.Mvc.SelectList(Staff, "StaffID", "FirstName"); }
}//end

view

@Html.DropDownListFor(m => m.SelectedStaffId, Model.StaffItems, "--Who is taking appointment--", new { @class = "form-control", @id = "ddlValuer" })

Now I want to bind a variable number of dropdownlists. I expected to achieve this would be a simple loop as below: (BUT THIS DOESN'T WORK)

ViewModel

//for offerState ddl
public List<OfferState> OfferStates { get; set; }
public int? SelectedOfferStateId { get; set; }
public IEnumerable<System.Web.Mvc.SelectListItem> OfferStateItems
{
    get { return new System.Web.Mvc.SelectList(OfferStates, "OfferStateID", "Description"); }
}//end

View

for (var i = 0; i < Model.Offers.Count; i++)
{
    <div class="row">
        <div class="col-lg-4">@Html.TextBoxFor(m => Model.Offers[i].Offer1, null, new { @class = "form-control", @disabled = "disabled" })</div>
        <div class="col-lg-8">@Html.DropDownListFor(m => Model.Offers[i].OfferStateID, Model.OfferStateItems, "--State--", new { @class = "form-control" })</div>
    </div>
}

The items are never selected, --state-- (the default text), is always displayed. I have stepped through the code and can see there are three items with an OfferStateID.

Why is the default value always selected? Is there a better way?

1

2 Answers 2

1

This is a known limitation of using DropDownListFor() in a loop. You need to build a new SelectList in each iteration, or use an EditorTemplate and pass the SelectList to the template as AdditionalViewData

for (var i = 0; i < Model.Offers.Count; i++)
{
    @Html.DropDownListFor(m => m.Offers[i].OfferStateID, new SelectList(Model.OfferStateItems, "OfferStateID", "Description", Model.Offers[i].OfferStateID), "--State--", new { @class = "form-control" })
}

where OfferStateItems is IEnumerable<OfferStates> (not SelectList)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the suggestion. I have tried it and my problem persists
0

In your model just change this:

 public IEnumerable<System.Web.Mvc.SelectListItem> OfferStateItems
{
    get { return new System.Web.Mvc.SelectList(OfferStates, "OfferStateID",     "Description"); }
}//end

To this

public List<OfferStates> OfferStateItems{get;set;}

Then create a constructor for your ModelView

public yourViewModel()
{
      this.OfferStateItems=(place here the method to populate the list)
}

Then you can use that list with the items you need, and use a HTML helper to write the items.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.