Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

c# - ASP.NET, MVC ListBoxFor Edit "The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."

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.

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:

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

On this code:

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

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

Model:

public string Mask_Concat { get; set; }

View:

                <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 on view:

//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(","));
});

Controller:

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);
}

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

Thoughts? Need more information?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...