3

I have something like this in my code and I am getting error: Exception Details: System.ArgumentException: Value cannot be null or empty. Parameter name: name . What am I doing wrong ? Thanks for help

@model IEnumerable<NHibernateFluentProject.Patient>

@Html.ListBoxFor(model => model, new SelectList(Model,"ID", "FirstName"));

1

2 Answers 2

6

@Html.ListBoxFor is used for your strong typed viewmodel. which could help to bind to your property. First part will take a lambda expression for a single item as a default seleced for your listbox, second part will take the item collections to dispaly all the listbox items. For example: you have following two classes.

public class HospitalViewModel
{
    public string SelectedPatient { get; set; }
    public IEnumerable<Patient> AllPatients { get; set; }
}
public class Patient
{
    public int Id { get; set; }
    public string FirstName { get; set; }
}

From you view, you should do something like

@model HospitalViewModel

@Html.ListBoxFor(model => model.SelectedPatient, new SelectList(Model.AllPatients,"Id", "FirstName"));

OR if you only want to bind all your patients to a listbox, then use Html.ListBox instead

@model IEnumerable<Patient>

@Html.ListBox("ListBoxName", new SelectList(Model,"Id", "FirstName"));
Sign up to request clarification or add additional context in comments.

3 Comments

I'm looking for something like your answer, but instead of string SelectPatient is there a way to have a collection of multiple selections? So if the user selects more than one patient they'll show up in some List of collection. Any ideas?
@NullReference: in your case, you may need a Ajax call to send a json object to include all your selected ListItems from the client side to the controller to handle it.
Thanks for the response Allen. I actually found the answer here stackoverflow.com/questions/3194143/….
0

You need to pass a lambda expression containing the property to bind the listbox to.

1 Comment

@petro: m => m.SomeProperty

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.