1

I have bound listbox data from entity framework. I have selected multiple values in that listbox. But, the values are not fired. The count of the property is 0 only. I am using the code below:

public class Sample1  
{  
[Key]  
public int SampleId{ get; set; }  
public string SampleDesc{ get; set; }  
}
public class ExpModel  
{  
public List<Sample1> Sample{ get; set; }  
}
public ActionResult Index()  
{       
ViewData["SampleList"] = new List<Sample1>(entity.samp);  
return View();  
} 
@Html.ListBoxFor(model => model.Sample, new  SelectList(((List<Details.Models.Sample1>)ViewData["SampleList"]), "SampleId", "SampleDesc")) 

What do I have to do? Please help me...

1
  • Please post your Model and your Controller Action. Commented Aug 7, 2013 at 14:19

1 Answer 1

0

You should bind the ListBoxFor helper to a property that is a collection of simple/scalar values such as strings or integers:

public class Sample1  
{  
    [Key]  
    public int SampleId { get; set; }  
    public string SampleDesc { get; set; }  
}

public class ExpModel  
{  
    public List<int> SelectedSampleIds { get; set; }  
}

and then:

public ActionResult Index()  
{       
    ViewData["SampleList"] = new List<Sample1>(entity.samp);
    return View();  
}

and in your view:

@Html.ListBoxFor(
    model => model.SelectedSampleIds, 
    new SelectList(
        (List<Details.Models.Sample1>)ViewData["SampleList"], 
        "SampleId", 
        "SampleDesc"
    )
)

Now when you submit the form, the SelectedSampleIds collection will contain the selected ids in the list box.

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

Comments

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.