【问题标题】:DropDownListFor does not select value if in for loop如果在 for 循环中,DropDownListFor 不选择值
【发布时间】:2017-03-10 01:34:45
【问题描述】:

在我看来

<%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%>

在我的控制器中

public int[ ] Countries { get; set; }

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

当表单发布没有问题时,下拉列表被填充并且用户选择的值被发布。但是,当我尝试加载已经为国家 [] 分配值的表单时,它不会被选中。

【问题讨论】:

  • 11 年后我仍然对此表示赞同,这真的还是个问题吗?
  • 非常如此。只花了 30 分钟来追踪这个

标签: asp.net-mvc asp.net-mvc-2 drop-down-menu html-helper


【解决方案1】:

不确定这是否是 mv4 的新功能,或者它是否存在于以前的版本中。但是 DropDownListFor 包含一个用于 SelectList 构造函数的附加参数。

SelectList(IEnumerable, String, String, Object)

例如:

Html.DropDownListFor( x => x.Countries[ i ], New SelectList(Model.CountryList,"ID","Description",Model.Countries[i]))

其中IDCountryList 对象中的国家/地区ID,Description 是国家/地区名称。

【讨论】:

  • +1 让它为我工作。我仍然不想为循环中的每个元素重新创建我的 SelectList,所以我会继续搜索。
  • 谢谢!对我来说效果很好,可以在表格的每一行中显示一个下拉列表(位置 ID)。 @Html.DropDownListFor(x=&gt;x.OrderLineItems[i].OrderMfgLocationId, new SelectList(Model.MfgLocations, "Value", "Text", Model.OrderLineItems[i].OrderMfgLocationId))
  • 不选择列表中的匹配项 - 将新项添加到列表顶部。可用,但不理想。
【解决方案2】:

我也一样。当使用 foreach 循环 DropDownListFor 时(即在页面上呈现多个选择元素)。

我的解决方法是在控制器而不是视图中设置选定的值:像这样:

在控制器中:

public class FruitList 
{        
    public int? selectedFruit{ get; set; }
    public List<SelectListItem> fruits
    {
        get
        {
            fruitEntities F = new fruitEntities();
            List<SelectListItem> list = (from o in F.Options
                                         select new SelectListItem
                                         {
                                             Value = o.fruitID,
                                             Text = o.fruit,                                                 
                                             Selected = o.fruitID == selectedFruit
                                         }).ToList();
            return list;
        }
    }
}

public class ViewModel 
{              
    public List<FruitList> collectionOfFruitLists { get; set; }        
}

在视图中

<table>                        
   <% for (int i=0; i < Model.collectionOfFruitLists.Count; i++ )
        { %>
        <tr>                            
            <td><%: Html.DropDownList("fruitSelectList", collectionOfFruitLists[i].fruits, "Please select...") %></td>                
        </tr>
        <%} %>
 </table>

控制器中的漂亮位是Selected = o.fruitID == selectedFruit,它的作用类似于SQL CASE 语句; Lance Fisher 很好地解释了这一点(感谢 Lance,你的帖子真的帮助了我:)

【讨论】:

  • 您不需要? true : false 位,只需Selected = o.fruitID == selectedFruit 即可。
  • B 先生 - 解决问题的好人。运行您的代码时出现 StackOverflow 错误,但这个想法是合理的 - 您必须为每个下拉列表创建 SelectList 并在控制器中显式设置 SelectedItem。不错的作品。这让我发疯了!
  • 只想说,截至今天(2016 年)使用 MVC5,这仍然是一个问题......这是其中之一“但是为什么??”在宇宙中迷失了,当你从逻辑上认为它应该工作但没有明显的原因时却没有。你会认为微软会说“顺便说一句”,但也没有。谢谢stackoverflow!我会尝试下定决心这样做...... ughhh
【解决方案3】:

我知道这个问题有点老了,但我只是在循环遍历对象列表并尝试将值绑定到我的编辑视图中的 DropDownListFor(s) 时遇到了这个问题。

我通过使用其他人针对此问题给出的一些先前解决方案的逻辑,通过内联解决方案克服了这个问题。

绑定到我的模型:

@Html.DropDownListFor(model => model.QuestionActions[i].QuestionActionTypeId,
      Model.QuestionActionTypes.Select(x => new SelectListItem() { Value = x.Value, Text = x.Text, Selected = (x.Value == Model.QuestionActions[i].QuestionActionTypeId.ToString()) }).ToList(),
          "Select Action Type",
                 new { })

Model.QuestionActionTypes 是我的 Controller 中填充的 SelectList。

【讨论】:

  • 聪明!我花了几个小时试图找到这样的解决方法。
【解决方案4】:

这有点小技巧,并且依赖于 JavaScript,但对我来说效果很好。

您需要知道将由字段生成的客户端 ID(通常这些可以手动计算,但为了安全起见,您可能需要使用类似 FieldIDFor method 的东西。

您只需要根据模型设置值,在 jQuery 的 $(document).ready 中:

<script type="text/javascript">
    $(document).ready(function () {
        @for(var i = 0; i < 5; i++)
        {
            <text>
                $("#@Html.FieldIDFor(m => m.Countries[i])").val("@Model.Countries[i]");
            </text>
        }
    });
</script>

【讨论】:

    【解决方案5】:

    选择框发送单个值,因此Countries 属性不应是数组。同样在您的帖子中,不清楚您在 lambda 表达式中使用的 i 变量来自哪里,并且在帮助程序中使用的 x.CountryList 不会编译,因为 x 未定义。

    型号:

    public class MyModel
    {
        public int SelectedCountry { get; set; }
        public List<SelectListItem> CountryList { get; set; }
    }
    

    查看:

    <%= Html.DropDownListFor(x => x.SelectedCountry, Model.CountryList) %>
    

    更新:

    根据评论,似乎有多个下拉菜单。我怀疑问题可能来自在for 循环中用作索引的i 变量。

    你可以试试这个:

    型号:

    public class MyModel
    {
        public int[] Countries { get; set; }
        public List<SelectListItem> CountryList { get; set; }
    }
    

    查看:

    <% foreach (var country in Model.Countries) { %>
        <%= Html.DropDownListFor(x => country, Model.CountryList) %>
    <% } %>
    

    【讨论】:

    • 我认为“i”不是问题,因为当我发布表单时,下拉列表中的值会正确发布到 Country[] 属性中。
    • "i" 对于我们这些正在回发结果并需要将集合绑定到服务器上的人来说是必需的。
    【解决方案6】:

    不要在视图模型中使用 IEnumerable,而是使用像这样的对象列表:

    public List<PairVM<string,string>> TiposValorCobertura { get; private set; }
    

    在您看来,当您在循环中为下拉列表分配可选元素时,请这样做:

    @Html.DropDownListFor(m => m.Coberturas[i].TipoLimiteInferior, new SelectList(Model.TiposValorCobertura,"Key","Description",  Model.Coberturas[i].TipoLimiteIferior))
    

    其中“Key”和“Description”是 PairVM 的属性

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 2021-03-20
      相关资源
      最近更新 更多