Wednesday, 22 November 2017

Get ListBox Selected Text and Selected Value in Controller in MVC

Get ListBox Selected Text and Selected Value in Controller in MVC

Description:

In this example we explain that how to get ListBox Selected Text and Value in Controller in Asp.Net MVC.or how to get Razor ListBox Selected Text and Value in Controller Action Method in MVC.or how to get multi select Listbox Text and Value in Controller in MVC.or how to get ListBox selected Item’s Text and value in Controller in MVC.or how to send ListBox selected Text value from Razor view to controller in MVC.or pass ListBox Selected Items from View to Controller in MVC.
Model:

public class EmployeeModel
    {
        public List<SelectListItem> Countries { get; set; }
        public int[] CountryIds { get; set; }
    }

Controller:

  public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            EmployeeModel emp = new EmployeeModel();
            emp.Countries = PopulateCountries();
            return View(emp);
        }

        [HttpPost]
        public ActionResult Index(EmployeeModel emp)
        {
            emp.Countries = PopulateCountries();
            if (emp.CountryIds != null)
            {
                List<SelectListItem> selectedItems = emp.Countries.Where(p => emp.CountryIds.Contains(int.Parse(p.Value))).ToList();

                ViewBag.Message = "Selected Countries:";
                foreach (var selectedItem in selectedItems)
                {
                    selectedItem.Selected = true;
                    ViewBag.Message += "\\n" + selectedItem.Text;
                }
            }

            return View(emp);
        }

        private static List<SelectListItem> PopulateCountries()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = " SELECT CountryName, CountryId FROM Country";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            items.Add(new SelectListItem
                            {
                                Text = sdr["CountryName"].ToString(),
                                Value = sdr["CountryId"].ToString()
                            });
                        }
                    }
                    con.Close();
                }
            }

            return items;
        }
    }

View: 

@model ListBox_MVC.Models.EmployeeModel

@{
    Layout = null;
}

<html>
<head>
    <title>ASP.Net MVC: Get ListBox Selected Text and Selected Value in Controller</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        @Html.Label("Country:")
        <br/>
        <br/>
        @Html.ListBoxFor(m => m.CountryIds, Model.Countries)
        <br/>
        <br/>
        <input type="submit" value="Submit"/>
    }
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert("@ViewBag.Message");
            };
        </script>
    }
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment