8

I have a list of type stored procedure which have an ID and a Name as data in it. i have property of int type in model and a list of same stored procedure. now i want to bind this information into ListBoxFor

in view i have written this

@Html.ListBoxFor(x => x.HobbyId, new MultiSelectList(Model.listHobby, "pkHobbyId", "Hobby"))

but i am getting an error

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

Please Help how to bind.

1
  • Please post also the Model. What type is listHobby?. How you set values to it?. Commented Apr 5, 2012 at 12:58

3 Answers 3

7

try that

@Html.ListBoxFor(x => x.HobbyId, Model.listHobby.Select(f => new SelectListItem { Text = f.Hobby, Value = f.pkHobbyId.ToString() }), new { Multiple = "multiple" }) 

listHobby is iEnumerable list on my sample


sorry if i mislead you, rushed to answer but you cannot get the result of the multiselect listbox into a guid or int variable (whatever type is your HoobyId is) you should have an array to grab the result like

public string[] SelectedHobbyIds { get; set; }

so there must be something wrong with your View Models so its better that u would post your view models to be checked

Sign up to request clarification or add additional context in comments.

1 Comment

yes i get it i m returning the value in hobby id which is of int whereas i have to return in a collection.
5

@ Chhatrapati Sharma,

In your controller, try this,

ViewData['anyName'] = new SelectList {
 Text = ,  // text from ur function
 Value = , // Value from function
 Selected = // if required
}

and in view, bind the viewdata like,

 <@Html.ListBox("docImages", ((IEnumerable<SelectListItem>)ViewData["anyName"]))

For testing, try a sample selectlist item as follows,

ViewData['anyName'] = new List<SelectListItem>{ 
                new SelectListItem {Text = "First", Value = "0"}, 
                new SelectListItem {Text = "Second"), Value = "1"}, 
                new SelectListItem {Text = "Third", Value = "2"} 
                    };

If this sample works, then check your function "_supp.listDocImages()" and make sure it return IList

Comments

3
@Html.ListBoxFor(x => x.HobbyId, Model.listHobby.Select(f => new SelectListItem { Text = f.Hobby, Value = f.pkHobbyId.ToString() }), new { Multiple = "multiple" }) 

HobbyId in expression must be ienumerable because you set multi select

1 Comment

it didn't work for me // controller public ActionResult AddEditSupplier(int id) { clsSupplier _supp = new clsSupplier(); _supp.lstDocImgs = _supp.listDocImages();// it's working fine _supp.lstDocFiles = _supp.listDocFiles(); // it's working fine return View(_supp); } //View but in view both are not working @Html.ListBoxFor(x => x.DocumentId, new selectList(Model.lstDocImgs, "documentid", "title")) @Html.ListBoxFor(x => x.DocumentId, Model.lstDocImgs.Select(f => new SelectListItem { Text = f.Title, Value = f.documentid.ToString() }), new { Multiple = "multiple" })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.