2014-11-15 110 views
0

你好,我想用下拉列表搜索方法

這是它做在我的數據庫搜索:

 @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); 
    } 

當用戶將選擇位置的數據將按位置排序

問題是當我嘗試點擊搜索對接在沒有任何事情發生(它就像是值爲空)

+0

即使我想補充一個httppost方法沒有什麼happend – saidmohamed11

回答

3

您需要添加表單元素。沒有形式元素,什麼都不會冒險。

<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" /> 
} 
+0

哇!我忘了添加............ @使用(Html.BeginForm()) { 想你我的朋友 – saidmohamed11