Wednesday, January 1, 2014

Get the values of multiselect Listbox in postback - ASP.NET MVC

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
@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"/>
}




C#
  [HttpGet]
        public ActionResultList()
        {
            var list = new List<SelectListItem>();
          
            foreach (Subjectsub in context.Subjects)
           {
               SelectListItem select = new SelectListItem() {
                   Text=sub.name,
                   Value=sub.id.ToString(),
                   Selected=sub.isselected.Value};
               list.Add(select);
           }

            SubjectListView view = newSubjectListView();
            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";
            }
        }




Model

    public class SubjectListView
    {

        public IEnumerable<string> SelectItems { set; get; }

        public List<SelectListItem> Subjects { set; get; }
    }



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.

1 comment:

  1. Developer Blog: Get The Values Of Multiselect Listbox In Postback - Asp.Net Mvc >>>>> Download Now

    >>>>> Download Full

    Developer Blog: Get The Values Of Multiselect Listbox In Postback - Asp.Net Mvc >>>>> Download LINK

    >>>>> Download Now

    Developer Blog: Get The Values Of Multiselect Listbox In Postback - Asp.Net Mvc >>>>> Download Full

    >>>>> Download LINK TN

    ReplyDelete