Dropdownlist based filtering in MVC 3 using EntityFrameWork(EDM) Modalfirst Method

SomeTimes We Need To Filter our items using dropdownlist we did it in asp.net let’s see how to do it in mvc using entity frame work Modalfirst method

To Implement Model First Method First We need to create the DataBase

Create Datatable as follows

And insert data in table as follows

Then create new project as follows

Open VisualStudio2010–> New Project –> asp.net mvc 3 web application

Then One popup window will come select in that as shown below

Then add images folder under content folder and add .EDMX file to our Project

Don’t know how to add ClickHere

Then add _layout.cshtml file for adding css

Then add controller first

The write following action and  code in this controller

MvcApplication2.Models.MySampleDBEntities db = new Models.MySampleDBEntities();
    public ActionResult SearchNames(string ddlcontent)
        var list = new List<string>();
        var nameqry = from n in db.Images
                      select n.PlayerName;
        ViewBag.ddlcontent = new SelectList(list);
        var names = from m in db.Images
                    select m;
        if (string.IsNullOrEmpty(ddlcontent))
            return View(names);
            return View(names.Where(s => s.PlayerName.Contains(ddlcontent)));

After completion of this add required view as follows

Right click on action in controller and select add view then click on ok.

Now u have added view, then write following code in that view

@model IEnumerable<MvcApplication2.Models.Image>
ViewBag.Title = "SearchNames";

@using (@Html.BeginForm("SearchNames", "Names", FormMethod.Get))
@Html.DropDownList("ddlcontent", "All")<input type="submit" value="Filter" />;
<table border="4" style="border: medium dashed #FF0000">
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.PlayerName)
@Html.DisplayFor(modelItem => item.Play)
@Html.DisplayFor(modelItem => item.CountryName)
<img src="@item.ImagePath" height="100" width="100"/>

After completion this give default routing details as ur controller name and action name

After completion of all these run ur application

u can get output as follows


3 thoughts on “Dropdownlist based filtering in MVC 3 using EntityFrameWork(EDM) Modalfirst Method

  1. How would you do this if the dropdownlist was from one table and you wanted it to filter a different table where field == dropdownlist

  2. is ActionResult SearchNames(string ddlcontent) for [HttpPost]?

    where is is ActionResult SearchNames() for [HttpGet]?

    is it missing?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s