每个人,当我尝试过滤下拉列表时遇到问题。
情况是我希望使用相同视图/表单中的公司下拉列表过滤代理下拉列表。因为我需要显示仅属于所选公司的代理商。但我不知道那样做。
有任何解决方案吗?
答案 0 :(得分:1)
首先在Controller中添加Method,如下所示
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)));
}
然后将其绑定到视图,如下所示
@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>
答案 1 :(得分:0)
实际上有两个选项......要么将基于服务器的表单帖子分成多个步骤,要么使用jquery来处理这个行为JIT