Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to bind a Dropdown without using Model in Asp.Net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 412
    Comment on it

    How to bind a Dropdown without using Model in Asp.Net MVC

    For Binding the Dropdown without using Model, we have to follow the following steps:-

    Step1: Firstly in the Controller create an instance of List<selectlistitem> and add items to it. Then pass this List to the View with the help of ViewBag.
    For Example:</selectlistitem>

    public ActionResult Index()
            {
                List<SelectListItem> lst = new List<SelectListItem>();
                lst.Add(new SelectListItem
                {
                    Text = "Item1",
                    Value = "1"
                });
                lst.Add(new SelectListItem
                {
                    Text = "Item2",
                    Value = "2"
                });
                lst.Add(new SelectListItem
                {
                    Text = "Item3",
                    Value = "2"
                });
                lst.Add(new SelectListItem
                {
                    Text = "Item4",
                    Value = "2"
                });
    
                ViewBag.SelectList = lst;
    
                return View();
            }
    

      As we know the Contrller is called first from the browser and then the View is rendered, therefore the Index() Action method will be called first in which we have created an instance of List<selectlistitem> and have added items into it, we then filled ViewBag.SelectList with this List and passed it to View </selectlistitem>

    Step 2: Now in the View we will bind this ViewBag to the DropDown as follows:

    @Html.DropDownList("SelectList", (IEnumerable<SelectListItem>)ViewBag.SelectList, "Select Value")
    

      This will bind the DropDown name 'SelectList' with ViewBag.SelectList with the default value 'Select Value'.

      For Postback of the Dropdown as we have SelectedIndexChange in Asp.Net we can add the following code:-

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new {id="Form" }))
    { 
    <div>
        @Html.DropDownList("SelectList", (IEnumerable<SelectListItem>)ViewBag.SelectList, "--Select Value---",
                new
                {
                    onchange = "document.getElementById('Form').submit();"  
                })
    </div>
    }
    

      This will redirect the control to the Action method described above in the Html.BeginForm in case of SelectedIndexChange.


    Note:=> we can also set the default value of the Dropdown in the Controller as well.

    Your Dropdown is ready for action...hope it helps...!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: