MVC DropDownListFor not selecting value from model

后端 未结 6 1247
逝去的感伤
逝去的感伤 2020-12-05 11:42

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 vi

相关标签:
6条回答
  • 2020-12-05 12:11

    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)) 
    
    0 讨论(0)
  • 2020-12-05 12:13

    I'm sending IEnumerable in SList instead of ViewBag

    @Html.DropDownListFor(model => Model.list[i].Id, new SelectList(Model.SList, "Value", "Text", Model.list[i].Id), htmlAttributes: new { @class = "form-control" })
    
    0 讨论(0)
  • 2020-12-05 12:14

    In my opinion one item of the ViewBag.ForeignLanguages should have Selected property set to true. DropDownListFor method select item based on ModelState. Here you have source code of DropDownListFor method an there is something like:

    object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string));
    

    And if that value is null then selected value is retrieved from Selected property

    0 讨论(0)
  • 2020-12-05 12:15

    I added an extension method to enforce the value selection. It also preserves view state data on a page reload.

    public static class SelectListExtensions
    {
        public static SelectList SelectValue(this SelectList selectList, object value)
        {
            return new SelectList(selectList, "Value", "Text", value);
        }
    }
    

    Usage is:

    selectList = selectList.SelectValue(someValue);
    
    0 讨论(0)
  • 2020-12-05 12:20

    I know this is an old question, but just in case someone is facing the same problem than me. In my case was that I accidentally was using a normal variable instead of a property in the model, as soon as I set it as a property instead it started working.

    0 讨论(0)
  • 2020-12-05 12:26

    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.

    0 讨论(0)
提交回复
热议问题