【问题标题】:MVC 5 Dropdownlistfor multiselect value binding in edit viewMVC 5下拉列表用于编辑视图中的多选值绑定
【发布时间】:2019-06-01 21:35:17
【问题描述】:

我在编辑视图中有一个 select2 多选下拉菜单。当我尝试将选定的值绑定到下拉列表时,它无法绑定。任何帮助表示赞赏。请从 *.cshtml 和 *.cs 文件中找到以下代码 sn-ps。

@Html.DropDownListFor(model => model.Items, new MultiSelectList(ViewBag.ItemsBag, "Value", "Text", Model.ItemsSelected.Select(x => x.Value)), new { @class = "form-control features-segments select2-multiselect-checkbox", multiple = "multiple" })

ViewBag.ItemsBag = db.Items.Select(v => new SelectListItem
            {
                Text = v.ItemName,
                Value = v.ItemId.ToString()
            });

ModelVM modelVM = new ModelVM()
            {
                ItemsSelected = SelectedItems.Items.Select(x => new SelectListItem() { Text = x.ItemName, Value = x.ItemId.ToString() })
            };

Items 模型具有以下项目。属性 ItemsSelected 不为空,其中包含 3 个值,ViewBag.ItemsBag 也不为空,并且包含数据库中的所有项目。这两个属性都是具有 Text 和 Value 属性的 SelectListItem 类型。

public int FeatureId { get; set; }
public string FeatureName { get; set; }
public string ReferenceName { get; set; }
public FeatureSection SectionName { get; set; }//Enum
public FeatureType Type { get; set; }//Enum
public bool DefaultBoolValue { get; set; }
public string DefaultTextValue { get; set; }
public IEnumerable<SelectListItem> ItemsSelected { get; set; }
public virtual ICollection<Item> Items { get; set; } = new List<Item>();

【问题讨论】:

  • 尝试将DropDownListFor (DDLF) 替换为ListBoxFor (LBF),因为DDLF 总是设置allowMultiple = false 而LBF 使用allowMultiple = true
  • @TetsuyaYamamoto 感谢您的快速建议。我也尝试了 ListBoxFor,但需要帮助。
  • 你能在视图模型中显示Items 的类型吗?请进一步解释选择元素如何“绑定失败”,它是 null 还是仅包含单个选定值?
  • 对不起@TetsuyaYamamoto,我已经更新了这个问题。模型项具有值(非空),甚至单个项都没有绑定在下拉列表中。
  • 嗯,您的问题是绑定到Items 属性的下拉列表,该属性的类型为ICollection&lt;Item&gt;,这是一个复杂的对象,助手不支持。查看答案以找出问题所在。

标签: c# asp.net-mvc jquery-select2 dropdownlistfor


【解决方案1】:

由于您在ViewBag 定义中提供了这样的项目值,这清楚地表明了字符串值:

ViewBag.ItemsBag = db.Items.Select(v => new SelectListItem
{
    Text = v.ItemName,
    Value = v.ItemId.ToString() // implies all values are strings
});

那么与DropDownListFor/ListBox绑定的属性必须有List&lt;string&gt;string[]类型才能正确绑定。使用ICollection&lt;Item&gt; 不会绑定,因为它是一个复杂的对象,而helper 需要值类型(数值类型/字符串)才能绑定。

因此,您必须首先创建一个类型为List&lt;string&gt; 的属性:

public List<string> SelectedValues { get; set; }

然后将 ListBoxFor 助手与该属性一起使用:

@Html.ListBoxFor(model => model.SelectedValues, new MultiSelectList(ViewBag.ItemsBag, "Value", "Text", Model.ItemsSelected.Select(x => x.Value)), new { @class = "form-control features-segments select2-multiselect-checkbox", multiple = "multiple" })

注意:

如果ItemId 属性具有int 类型(并且所有值都可以转换为int),请尝试使用List&lt;int&gt;/int[] 类型而不是List&lt;string&gt;/string[]

public List<int> SelectedValues { get; set; }

【讨论】:

    【解决方案2】:

    请在 jQuery 的文档就绪状态下尝试以下代码:

    var result = [1,3,5];// Array of SelectedValues
    $("#DropdownID").val(result); // DropdownID = your multi select dropdown Id
    

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      相关资源
      最近更新 更多