Multiple Html.dropdownlistfor with the same selectlist, set selected value

Refresh

April 2019

Views

2.6k time

1

I have a Add/Edit View with several dropdownlistfor, all is populated from the same selectlist. When i try to populate the view for editinng all dropdownlist have the same selected as the first one. Is there a way around this? I don't wanna use multiple selectlist because they all have the same content.

Most of the dropdownlist is create from a list with one dropdownlist per item. This the first part of the view that workes its not created from list of item.

        <div class="control-group">
            @Html.LabelFor(model => model.FirstRound, new { @class = "control-label" })
            <div class="controls">
                @Html.DropDownListFor(model => Model.FirstRound.Id, Model.Difficulties, new { @class = "span3 combobox"})
                @Html.TextBoxFor(model => Model.FirstRound.Value, new { @class = "input-xlarge span1 sec-val", @readonly = "readonly"})
                @Html.ValidationMessageFor(model => model.FirstRound.Value, null, new { @class = "help-inline" })
            </div>
        </div>

This is the part where i loop the list of item to create dropdownlists.

@{int counter = 0;}
        @foreach (var item in Model.SecondRound)
        {

            <div class="control-group">
                @Html.LabelFor(model => model.SecondRound, new { @class = "control-label" })
                <div class="controls second-round">
                    @Html.DropDownListFor(model => model.SecondRound[counter].Id, Model.Difficulties, new { @class = "span3 combobox", @id = "SecondRound[" + counter + "].Id" })
                    @Html.TextBoxFor(model => Model.SecondRound[counter].Value, new { @class = "input-xlarge span1 sec-val", @readonly = "readonly"})
                    @Html.ValidationMessageFor(model => Model.SecondRound[counter].Value, null, new { @class = "help-inline" })
                </div>
            </div>
            <hr />
            counter = counter + 1;
        }

This is my ViewModel

public class AddTariffTrampetVM
{
    public string Team { get; set; }
    [HiddenInput(DisplayValue=false)]
    public int NumberOfGymnasts { get; set; }

    [Display(Name="Difficulty")]
    public RoundVM FirstRound { get; set; }
    [Range(0.001d, 100.0d, ErrorMessage="You need to pick a difficulty for the first round!")]
    public decimal FirstTotalValue { get; set; }

    [Display(Name = "Difficulty")]
    public IList<RoundVM> SecondRound { get; set; }
    [Range(0.001d, 100.0d, ErrorMessage = "You need to pick difficulties for the second round!")]
    public decimal SecondTotalValue { get; set; }

    [Display(Name = "Difficulty")]
    public IList<RoundVM> ThirdRound { get; set; }
    [Range(0.001d, 100.0d, ErrorMessage = "You need to pick difficulties for the third round!")]
    public decimal ThirdTotalValue { get; set; }

    public List<SelectListItem> Difficulties { get; set; }

    public AddTariffTrampetVM() : this(6)
    {
    }

    public AddTariffTrampetVM(int numOfGymnast)
    {
        NumberOfGymnasts = numOfGymnast;

        FirstRound = new RoundVM { Id = 0, Value = 0M };
        SecondRound = new List<RoundVM>();
        ThirdRound = new List<RoundVM>();
        for (int i = 0; i < NumberOfGymnasts; i++)
        {
            SecondRound.Add(new RoundVM());
            ThirdRound.Add(new RoundVM());
        }
    }
}

public class RoundVM {
    public int Id { get; set; }
    [Range(0.001d, 100.0d, ErrorMessage="Your need to pick a difficulty!")]
    public decimal Value { get; set; }


    public RoundVM()
    {
        Value = 0M;
    }
}

This is my Edit action to fill the form

    public ActionResult Edit(int id)
    {
        var item = ServiceTariff.GetTariffTrampet(id);
        var model = Mapper.Map<TariffTrampet, AddTariffTrampetVM>(item);
        model.Difficulties = ServiceDifficulty.GetSelectListElementTrampet().ToList();

        return View("Add", model);
    }

Hope someone can help me with this. If you need any more code just comment.

1 answers

4

Instead of just providing Model.Difficulties to the DropDownListFor helper try passing it:

new SelectList(Model.Difficulties, object selectedValue)

OR another overload that lets you specify the value and text field:

new SelectList(Model.Difficulties, string dataValueField, string dataTextField, object selectedValue)

Use one of these as the second argument for the DropDownListFor helper. Creating a SelectList from the List<SelectListItem> allows you to set the initially selected value.