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;
        list.AddRange(nameqry.Distinct());
        ViewBag.ddlcontent = new SelectList(list);
        var names = from m in db.Images
                    select m;
        if (string.IsNullOrEmpty(ddlcontent))
            return View(names);
        else
            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";
}

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

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

DownloadSampleCode

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s