How to filter the dropdown list by using other dropdown list in asp.net MVC web-application
everyone, I encounter a problem when I try to filter the drop down list.
The situation is I wish to filter the开发者_运维知识库 Agent dropdown list by using the Company dropdown list in same view/form. Since I need to display the Agents that only belong to the selected Company. But I have no idea to do that.
Any solution, please?
First add Method in Controller as follows
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//Filter content basedon dropdownlist selected item
return View(names.Where(s => s.PlayerName.Contains(ddlcontent)));
}
then bind it to view as follows
@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>
2 options really...either split the server-based form post into multiple steps, or use jquery to handle this behavior JIT
精彩评论