Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC DropDownListFor not selecting value from model

I have read through this question ASP.NET MVC DropDownListFor not selecting value from model and answer but I don't know the solution to my problem.

This is my view:

@foreach (var adjusterLanguages in Model.adjusterLanguages)
{
    <div class="editor-field row">
        @Html.DropDownListFor(model => Model.adjusterLanguages[i].languageID,
                              (SelectList)ViewBag.ForeignLanguages) 
        @Html.ValidationMessageFor(model =>Model.adjusterLanguages[i].languageID)
    </div>

    <div style="clear: left;"></div>

    i++;
}

I can confirm that the model data is being populated (from my controller):

model.adjusterLanguages = adjLangs;

So, how can I make it select the items when the model gets sent to the view?

The generated html is as follows:

<div class="editor-field row">
    <select id="adjusterLanguages_0__languageID" name="adjusterLanguages[0].languageID"><option value="">--Select--
        </option>
        <option value="94c5be88-814e-4719-9784-587eb88aa975">Afrikanns</option>
        <option value="37b7bf71-7f4e-47c2-8b81-652b0d63dbcb">Albanian</option>
        <option value="c94d6d85-0ba2-47d7-932e-2a61feba37b3">Arabic</option>
        <!-- ... truncated for brevity ... -->
    </select> 
    <span class="field-validation-valid" data-valmsg-for="adjusterLanguages[0].languageID" data-valmsg-replace="true"></span>
</div>
<div style="clear: left;"></div>
<div class="editor-field row">
     <select id="adjusterLanguages_1__languageID" name="adjusterLanguages[1].languageID">
          <option value="">--Select--</option>
          <option value="94c5be88-814e-4719-9784-587eb88aa975">Afrikanns</option>
          <option value="37b7bf71-7f4e-47c2-8b81-652b0d63dbcb">Albanian</option>
          <option value="c94d6d85-0ba2-47d7-932e-2a61feba37b3">Arabic</option>
          <!-- ... truncated for brevity ... -->
     </select> 
     <span class="field-validation-valid" data-valmsg-for="adjusterLanguages[1].languageID" data-valmsg-replace="true"></span>
</div>
like image 794
user1477388 Avatar asked May 28 '13 19:05

user1477388


People also ask

What is difference between DropDownList and DropDownListFor?

DropdownListFor is support strongly type and it name assign by lambda Expression so it shows compile time error if have any error. DropdownList not support this.

What is Html DropDownList?

Definition and Usage. The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted) ...


2 Answers

I found the answer! Thanks to all for your help. When I change my code to the following, it works. The code simply specifies the selected value:

@Html.DropDownListFor(
   model => Model.adjusterLanguages[i].languageID, 
   new SelectList(
       ViewBag.ForeignLanguages, "Value", "Text", 
       @Model.adjusterLanguages[i].languageID)) 
like image 66
user1477388 Avatar answered Sep 22 '22 03:09

user1477388


When you're populating ViewBag.ForeignLanguages in the controller, on the item that should be selected, set the .Selected property to true.

However, you'll have an issue with this because you have many items using the same ForeignLangauges list of items.

You'll need to create one list for each adjusterLanguages in your list. They cannot share the same list because adjusterLanguages[0] needs a different item selected than adjusterLanaguages[1]

Edit:

I like this model:

public class AdjusterLanguageModel
{
  public Guid LanguageId { get; set; }
  List<SelectListItem> ForeignLanguages{ get; set; }
}

public class AdjusterListModel
{
  public List<AdjusterLanguageModel> AdjusterLanguages { get; set }
}

In the controller, you populate your list with an ID and it's own list of available languages.

In your view:

@foreach (var adjusterLanguages in Model.AdjusterLanguages)
{
    <div class="editor-field row">
        @Html.DropDownListFor(model => adjusterLanguages[i].LanguageID,
          adjusterLanguages[i].ForeignLanguages) 
        @Html.ValidationMessageFor(model => adjusterLanguages[i].LanguageID)
    </div>

    <div style="clear: left;"></div>

    i++;
}

So each DropDownListFor get's it's own list of languages.

like image 34
Matt Houser Avatar answered Sep 21 '22 03:09

Matt Houser