class Tag
{
public int Id {get;set;}
public string Name {get;set;}
}
class Book
{
public List<Tag> Tags {get;set;}
public Book()
{
Tags = new List();
}
}
List<Tag> list = new List();
list.Add(new Tag{Name="A"});
list.Add(new Tag{Name="B"});
list.Add(new Tag{Name="C"});
var c = new Book();
ViewBag.TagList = list;
return View(c);
Your View:
@Html.ListBoxFor(model => model.Tags, new SelectList(ViewBag.TagList, "Id", "Name"), new { @class = "listbox", style = "height:500px;" })
sudhiprclind...
Member
82 Points
54 Posts
Multi Selected List Box
Jul 28, 2012 05:47 AM|LINK
How we implement multiselected list box in mvc3 razor ?
am created one model, controller and view for that but i dont no how i take selected values from the multiselected list box using c#.
please advice.
model C mvc ListBox
Sudheesh
Yorrick vd V...
Participant
1674 Points
301 Posts
Re: Multi Selected List Box
Jul 28, 2012 11:30 AM|LINK
Hi,
Maybe this will help you on your way: http://stackoverflow.com/questions/7816246/jquery-multiselect-dropdownlist-how-to-access-results
Hope it helps.
Regards,
Yorrick
&
Don't forget to click "Mark As Answer" on the post that helped you.
CPrakash82
All-Star
18270 Points
2839 Posts
Re: Multi Selected List Box
Jul 28, 2012 02:14 PM|LINK
You need to follow below steps.
Create list property in your model, this type should have some key value pair value. e.g.
public class Student
{
public int Id {get;set;}
public string Name {get;set;}
}
public class MyModel
{
public List<Student> Students {get;set;}
public List<string> SelectedStudent {get;set;}
}
In your view use ListBox html helper to render the view.
@Html.ListBoxFor(m => m.SelectedStudent, new MultiSelectList(Model.Students, "Id", "Name"), new {Multiple = "multiple"})
In your controller initialize the model and send the list of student, on post you will recieve the SelectedStudent from view.
model C mvc ListBox
khoimk
Member
83 Points
28 Posts
Re: Multi Selected List Box
Jul 28, 2012 06:17 PM|LINK
Your Controller and Action:
class Tag { public int Id {get;set;} public string Name {get;set;} } class Book { public List<Tag> Tags {get;set;} public Book() { Tags = new List(); } } List<Tag> list = new List(); list.Add(new Tag{Name="A"}); list.Add(new Tag{Name="B"}); list.Add(new Tag{Name="C"}); var c = new Book(); ViewBag.TagList = list; return View(c);Your View:
@Html.ListBoxFor(model => model.Tags, new SelectList(ViewBag.TagList, "Id", "Name"), new { @class = "listbox", style = "height:500px;" })Hope this help.
sudhiprclind...
Member
82 Points
54 Posts
Re: Multi Selected List Box
Jul 31, 2012 09:59 AM|LINK
R U SURE ?
Sudheesh