In this article we are going to see the how to get the values from the multi select Listbox, To get the values from the multi select we have to pass a IEnumerable of string to the first parameter, and the second parameter must have the IEnumerable of SelectListItem, To pass this both items we have to create a class template and make a strongly typed view.
View
C#
Model
Output
When click on Submit post back output will be 2,3
From this article you can learn how to pass the value from the multi select values from the listbox to the postback.
View
@model EmpApp.Models.SubjectListView
@{
ViewBag.Title = "List";
}
@using (Html.BeginForm())
{
@Html.ListBoxFor(x =>
x.SelectItems, Model.Subjects, new { size = 6
})
<br />
<input type="submit"
value="Submit"
/>
}
[HttpGet]
public ActionResult
List()
{
var list = new List<SelectListItem>();
foreach (Subject
sub in context.Subjects)
{
SelectListItem select = new SelectListItem()
{
Text=sub.name,
Value=sub.id.ToString(),
Selected=sub.isselected.Value};
list.Add(select);
}
SubjectListView view = new
SubjectListView();
view.Subjects = list;
return View(view);
}
[HttpPost]
public string List(IEnumerable<string>
SelectItems)
{
if (SelectItems != null)
{
return string.Join(",", SelectItems);
}
else
{
return "No
vaues are selected";
}
}
public class SubjectListView
{
public IEnumerable<string> SelectItems { set;
get; }
public List<SelectListItem> Subjects { set; get; }
}
When click on Submit post back output will be 2,3
From this article you can learn how to pass the value from the multi select values from the listbox to the postback.

This comment has been removed by a blog administrator.
ReplyDeleteany javascript is needed ?
ReplyDelete