Sunday, 22 June 2014

Bind DropDownList Using Entity Framework in ASP.Net MVC Using C#

6/22/2014 - By Pranav Singh 11

In this article I will show you how you can bind/populate dropdownlist using entityframework in asp.net mvc using C#. In this article I will also show you after binding how you can retrieve the selected value of  dropdownlist at controller end. This article you can use in MVC2/MVC3/MVC4/MVC5 application.


Now for this article first we will create a new mvc application. After creating application we will create model. In our model file we will add the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace bIND_Dropdownin_MVC.Models
{
    public class CountryModel
    {
        public SelectList CountryListModel { get; set; }
    }
}

Now just check the Entity Framework and sql table.


Entityframework



Now again come to solution explorer and add the controller class file. In your controller add the below code to bind the dropdownlist.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using bIND_Dropdownin_MVC.Models;

namespace bIND_Dropdownin_MVC.Controllers
{
    public class HomeController : Controller
    {
        //Bind/Populate DropDownList Using Entity Framework in ASP.Net MVC Using C#
        public ActionResult Index()
        {
            /*Create instance of entity model*/
            NorthwindEntities objentity = new NorthwindEntities();
            /*Getting data from database*/
            List<Country> objcountrylist = (from data in objentity.Countries
                                            select data).ToList();
            Country objcountry = new Country();
            objcountry.CountryName = "Select";
            objcountry.Id = 0;
            objcountrylist.Insert(0, objcountry);
            SelectList objmodeldata = new SelectList(objcountrylist, "Id", "CountryName", 0);
            /*Assign value to model*/
            CountryModel objcountrymodel = new CountryModel();
            objcountrymodel.CountryListModel = objmodeldata;
            return View(objcountrymodel);
        }

    }
}

Now we will create View for the model. In this view we will add all the HTML tags.

@model bIND_Dropdownin_MVC.Models.CountryModel
@{
    ViewBag.Title = "Bind/Populate DropDownList using Entity Framework in ASP.Net MVC Using C#";
}
<h2>
    Bind DrowpDownList</h2>
@Html.DropDownList("ddlcountry", Model.CountryListModel, new {@style="width:200px;" })

In above code we have first accessed the model. This model we will use to bind with the control. Now we have done run the page and see the output.



Now we will learn how we can retrieve the selected value at controller end. For this you needed to create post method in the controller. Now add the below post method in your controller.

[HttpPost]
        public ActionResult Index(int ddlcountry)
        {
            /*Create instance of entity model*/
            NorthwindEntities objentity = new NorthwindEntities();
            /*Getting data from database*/
            List<Country> objcountrylist = (from data in objentity.Countries
                                            select data).ToList();
            Country objcountry = new Country();
            objcountry.CountryName = "Select";
            objcountry.Id = 0;
            objcountrylist.Insert(0, objcountry);
            SelectList objmodeldata = new SelectList(objcountrylist, "Id", "CountryName", 0);
            /*Assign value to model*/
            CountryModel objcountrymodel = new CountryModel();
            objcountrymodel.CountryListModel = objmodeldata;

            /*Get the selected country name*/

            ViewBag.CountryName = objcountrylist.Where(m => m.Id == ddlcountry).FirstOrDefault().CountryName;


            return View(objcountrymodel);
        }

In above code you will see the index method parameter name is same as the dropdownlist in our view. You need  to keep both name same other wise you will not be able to get the selected  value.
Now run the page.


Now click on submit button you will see the selected country value in parameter.


Now press F5 and check the final output.




 DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

11 comments:

  1. Excelente Publicación

    ReplyDelete
  2. public SelectList CountryListModel { get; set; }

    IS this a Model? Do we need to create a Model named CountryListModel?

    ReplyDelete
    Replies
    1. Exactly the question I have! Could anyone please shed light on this?

      Delete
  3. Good code for dropdown in mvc.Thku for ur post

    ReplyDelete
  4. you need to change @Html.DropDownList("ddlcountry", Model.CountryListModel, new {@style="width:200px;" })
    to
    @Html.DropDownList("ddlcountry", Model.CountryListModel.Items, new {@style="width:200px;" })

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top