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 ?
ReplyDeleteworking
ReplyDeletethnx
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
dot net training in velachery | dot net training institute in velachery
Nice work, your blog is concept-oriented, kindly share more blogs like this
ReplyDeleteDot NET Online Training
I really liked your blog post.Much thanks again. Awesome.
ReplyDeletego langaunage training
azure training
java training
salesforce training
hadoop training
mulesoft training
https://titandijital.com.tr/
ReplyDeletebingöl parça eşya taşıma
kırşehir parça eşya taşıma
gümüşhane parça eşya taşıma
rize parça eşya taşıma
Y5Ä°