ASP.NET,MVC ListBoxFor Edit“当允许多个选择时,参数'expression'必须评估为IEnumerable." [英] ASP.NET, MVC ListBoxFor Edit "The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."

查看:249
本文介绍了ASP.NET,MVC ListBoxFor Edit“当允许多个选择时,参数'expression'必须评估为IEnumerable."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个我无法弄清的错误,甚至以前回答的问题也没有像我一样约束ListBox.

I am receiving an error that I cannot figure out and even previously answered questions are not binding the ListBox the same way I am.

这是我在创建视图上使用ListBox的方式: ASP.NET MVC返回逗号分隔的字符串从From ListBoxFor到控制器

Here is how I use the ListBox on my create view: ASP.NET MVC Return Comma Delimited String From From ListBoxFor To Controller

现在,当我进行编辑时,会出现此错误:

Now when I go to my edit I receive this error:

在以下情况下,参数表达式"必须计算为IEnumerable 允许多选.

The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed.

关于此代码:

 @Html.ListBoxFor(model => model.Mask_Concat, Enumerable.Empty<SelectListItem>(), new { @class = "chosen-container chosen-container-multi", @style = "width:300px" })

我已经尽我所能想到的一切,将ListBox绑定到ViewModel,仍然没有运气.

I have tried everything I can think of, binding the ListBox to a ViewModel, still no luck.

型号:

public string Mask_Concat { get; set; }

查看:

                <div class="fancy-form" id="mainMask">
                    @Html.LabelFor(model => model.Mask_Concat, "Mask(s)", new { @class = "control-label col-md-2" })
                    <div class="col-md-12">
                        @Html.ListBoxFor(model => model.Mask_Concat, Enumerable.Empty<SelectListItem>(), new { @class = "chosen-container chosen-container-multi", @style = "width:300px" })
                        @Html.ValidationMessageFor(model => model.Mask_Concat, "", new { @class = "text-danger" })
                    </div>
                </div>
                <input type="hidden" id="selectedMaskValues" name="selectedMaskValues" />

视图中的JS:

//join selected mask values
$("#Mask_Concat").chosen().change(function () {
    var $hidden = $("#selectedMaskValues");
    $hidden.val($(this).find('option:selected').map(function () {
        return $(this).val();
    }).get().join(","));
});

控制器:

public ActionResult Edit(string id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Chip_Master chipMaster = db.Chip_Master.Find(id);
    if (chipMaster == null)
    {
        return HttpNotFound();
    }
    ViewBag.Manufacturer = new SelectList(db.Chip_Master, "Mask_Concat", "Mask_Concat", chipMaster.Mask_Concat.Split(',').ToList());
    return View(chipMaster);
}

数据库: Mask_Concat列:1234,5678,2345,7890

Database: Mask_Concat Column: 1234,5678,2345,7890

有什么想法吗?需要更多信息吗?

Thoughts? Need more information?

推荐答案

ListBoxFor用于显示值的集合.您正在尝试将其映射到单个字符串值.我会在您的模型中添加一个属性,该属性将Mask_Concat拆分为一个数组并将 that 绑定到列表框:

ListBoxFor is used to display a collection of values. You're trying to map it to a single string value. I would add a property to your model that splits Mask_Concat into an array and bind that to the list box:

public string[] Mask_Concat_Values 
{ 
    get 
    {
        return Mask_Concat.Split(','); 
    }
    set 
    {
        Mask_Concat = string.Join(",",values);
    }
}

这篇关于ASP.NET,MVC ListBoxFor Edit“当允许多个选择时,参数'expression'必须评估为IEnumerable."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆