2016-07-05 71 views
-1

我想在我看來基於從兩個DropDownList給出的輸入顯示數據。什麼是最好的方法來做到這一點?我如何將DropDownList值傳遞給控制器​​?根據選定的下拉列表值重新獲取數據

這裏是我的控制器

// GET: Define 
     public ActionResult Index() 
     { 
      IEnumerable<SelectListItem> Rooms = db.RM_ROOM.Select(c => new SelectListItem 
      { 
       Value = c.NAME, 
       Text = c.NAME 

      }); 
      ViewBag.NAME = Rooms; 

      IEnumerable<SelectListItem> item = db.RM_ENTITY_TYPE.Select(c => new SelectListItem 
      { 
       Value = c.ENTITY_NAME, 
       Text = c.ENTITY_NAME 

      }); 
      ViewBag.ENTITY_NAME = item; 
      //var rooms = db.RM_ROOM.ToList(); 

      return View();  
     } 

這裏是我的指數

@using WebApplication1.Models; 

@{ 
    ViewBag.Title = "Index"; 
} 
<div class="container"> 
    <div class="row"> 
     <div class="col-sm-1"> 
      Room 
      @Html.DropDownList("NAME", "") 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-sm-2"> 
      Entity 
      @Html.DropDownList("ENTITY_NAME", "") 
     </div> 
    </div> 
</div> 

和我的視圖模型是

public class DefineViewModelcs 
{ 
    public IEnumerable<SelectListItem> Rooms { get; set; } 

    public int ROOMID { get; set; } 
    public string NAME { get; set; } 
    public IEnumerable<SelectListItem> Entities { get; set; } 
    public int ENTITY_TYPEID { get; set; } 
    public string ENTITY_NAME { get; set; } 
    public int APPROVED_ROOM_STATEID { get; set; } 
    public bool Selected { get; set; } 
} 
+2

對於您的視圖,您應該使用'@using WebApplication1.DefineViewModelcs.cs'而不是'WebApplication1.Models'來指定特定的視圖模型,如果它位於Models文件夾中,那麼它將是'WebApplication1。 DefineViewModelcs.cs' – prospector

+0

我已經在開始時使用過它,但並沒有在這裏複製它 –

回答

0

,你需要做的第一件事是通過你的價值的距離你對你的控制器的看法。

您可以使用Eventlistener來檢測下拉菜單中的更改,以便通過將下拉菜單包裝到窗體中來調用您的控件分級。

@using(Html.BeginForm("ActionName","ControllerName")) 
    { 
     @Html.DropDownList("IdOfDropDown", 
          "YourListToDisplayInsideTheMenu", 
          new { onchange = 「$(this.form).submit();」} 
    } 

您的控制器將需要接受輸入參數。 然後您將需要使用這些值來篩選您的數據庫中的搜索

您需要做的最後一件事是將您的搜索結果返回到您的視圖。

public ActionResult Filter(int id) 
     { 
      //Do your search here and return a list to the view, example: 
      Room room = db.RM_ROOM.Find(r => r.ROOMID == id) 

      return View(room); 

      //this will return a single room from your database 
     } 

我不知道你的模型如何一起工作100%,他們是如何定義的,但我希望這可以幫助你在你的方式來找到你的問題的理想解決方案。

相關問題