Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC 4 Html.ListBoxFor Model MultiSelectList IEnumerable Error?

Posted on 2014-01-13
4
Medium Priority
?
23,331 Views
Last Modified: 2014-01-19
I have successfully using the Html.ListBox with a ViewData like so:

[Action}
ViewData["GroupsList"] = new MultiSelectList(myGroups.GetAllGroups(), "GroupID", "GroupName");

[View]
@Html.ListBox("lbxGroups", (MultiSelectList)ViewData["GroupsList"], new { SelectionMode = "Single" })

Now I want to use the Model and having issues doing so.

I received this error when using Model.UserGroupList:

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

Note: the Action returns the correct data.

[Action]
public ActionResult Index()
{
       ManagerViewModel viewModel = new ManagerViewModel()
       {
                UserGroupsList = new MultiSelectList(myGroups.GetAllGroups(), "GroupID", "GroupName")
       };
            return View(viewModel);
}

[View]
@model ProjectUI.ManagerViewModel
@Html.ListBoxFor(model => model.GroupId, new MultiSelectList(Model.UserGroupsList, "Value", "Text"), new { SelectionMode = "Single" }))

[Models]
public class ManagerViewModel
{
        public int GroupId { get; set; }
        public MultiSelectList UserGroupsList { get; set; }
}
0
Comment
Question by:WorknHardr
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 2000 total points
ID: 39778720
Hi,

If you have single selection, then technically you may use dropdown. Please try following:

@Html.ListBoxFor(model => model.GroupId,
                new SelectList(Model.UserGroupsList, "Value", "Text"))


for more information, please refer to following:
http://code.msdn.microsoft.com/Using-the-DropDownList-67f9367d


Thanks
0
 

Author Comment

by:WorknHardr
ID: 39779504
Same error:
"The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."

Until.... I change the Action to SelectList from MultiSelectList like so:

     ManagerViewModel viewModel = new ManagerViewModel()
            {
                UserGroupsList = new SelectList(myGroups.GetAllGroups(), "GroupID", "GroupName")
            };

Q. Why do I find code snippets elsewhere using the MultiSelectList?
0
 

Author Comment

by:WorknHardr
ID: 39779733
Just tried this example and get the same working result as your suggestion, without using a SelectList in the view.

stackoverflow.com/questions/1255472/how-does-a-multiple-select-list-work-with-model-binding-in-asp-net-mvc

View model:
public class MyViewModel
{
    public int[] SelectedIds { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Controller:
public ActionResult Index()
{
    var model = new MyViewModel
    {
        // fetch the items from some data source
        Items = Enumerable.Select(x => new SelectListItem
        {
            Value = x.Id,
            Text = "item " + x.Id
        })
    };
    return View(model);
}

View:
@model MyViewModel
@Html.ListBoxFor(x => x.SelectedIds, Model.Items)
0
 

Author Closing Comment

by:WorknHardr
ID: 39792647
thx
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question