[英]search method in asp.net mvc
您好,我想使用下拉列表在数据库中进行搜索
就是这个 :
@Html.DropDownList("Id", new List<SelectListItem>{
new SelectListItem {Text="Agent name",Value="1"},
new SelectListItem {Text="Location",Value= "2",}
}, "choose",new { @class = "dropdown" })
<input type="submit" class="submit" value="Search" />
我的控制器:
public ActionResult Index(int Id=1)
{
var agentlocation = new AgentLocationViewModel();
if (Id == 2)
agentlocation.agents = db.Agents.OrderBy(a =>a
.Location.LocationName).ToList();
else
{
agentlocation.agents = db.Agents.ToList();
}
return View(agentlocation);
}
当用户选择位置时,数据将按位置排序
问题是当我尝试单击搜索按钮时,什么都没有发生(就像值是null)
您需要添加表单元素。 没有表单元素,将不会提交任何内容。
<form action="/Home/Index" method="get">
@Html.DropDownList("Id", new List<SelectListItem>{
new SelectListItem {Text="Agent name",Value="1"},
new SelectListItem {Text="Location",Value= "2",}
}, "choose",new { @class = "dropdown" })
<input type="submit" class="submit" value="Search" />
</form>
或使用剃刀:
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
@Html.DropDownList("Id", new List<SelectListItem>
{
new SelectListItem {Text="Agent name",Value="1"},
new SelectListItem {Text="Location",Value= "2",}
}, "choose", new { @class = "dropdown" })
<input type="submit" class="submit" value="Search" />
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.