Tuesday, 28 November 2017

Pass (Send) DropDownList Selected Text and Selected Value to Controller from View in MVC

ASP.Net MVC: Pass (Send) DropDownList Selected Text and Selected Value to Controller from View

Description:

In this example we explain that how to Pass DropDownList Selected Text and Value to controller in MVC.or how to send DropDownList selected Text and Value from razor View to Controller in MVC.or how to pass or send DropDownList Selected Items to controller in MVC.or how to access DropDownList Selected Text and Value from Controller in MVC.here we first bind or populate the DropDownList from Model and pass its selected text and value to controller in MVC.
Model:

public class HobbyModel
    {

        public List<SelectListItem> Hobbys { get; set; }
        public int? hobbyId { get; set; }
     
    }

Controller:

public class HobbyController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            HobbyModel hobby = new HobbyModel();
            hobby.Hobbys = PopulateHobbys();
            return View(hobby);
        }

        [HttpPost]
        public ActionResult Index(HobbyModel hobby)
        {
            hobby.Hobbys = PopulateHobbys();
            var selectedItem = hobby.Hobbys.Find(p => p.Value == hobby.hobbyId.ToString());
            if (selectedItem != null)
            {
                selectedItem.Selected = true;
                ViewBag.Message = "hobby: " + selectedItem.Text;
            }

            return View(hobby);
        }

        private static List<SelectListItem> PopulateHobbys()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = " SELECT hobby, hobbyId FROM Hobbys";
                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["hobby"].ToString(),
                                Value = sdr["hobbyId"].ToString()
                            });
                        }
                    }
                    con.Close();
                }
            }

            return items;
        }
   
    }

View:

@model DropDownListFor_MVC.Models.HobbyModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>ASP.Net MVC: Pass (Send) DropDownList Selected Text and Selected Value to Controller from View</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Hobby", FormMethod.Post))
    {
        <table>
            <tr>
                <td>
                    Fruit:
                </td>
                <td>
                    @Html.DropDownListFor(m => m.hobbyId, Model.Hobbys, "Please select")
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Submit"/>
                </td>
            </tr>
        </table>
    }
    @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