2014-01-17 125 views
0

我正在開發使用實體框架和asp.net mvc5的簡單功能。過濾下拉列表值

我要得到什麼:

我有數據庫字典的設備製造商。 並在網頁上我有2個下拉列表填寫製造商和產品名稱。 以完美的方式,它應該過濾「名稱」​​下拉列表,僅包含所選製造商的產品。

我的問題:

有,因爲我想獲得這個工作,而不使用JS或JS和其中一個下拉列表觸發動作是唯一的解決辦法過濾任何一個正確的和簡單的方法?

如何我從數據庫中讀取數據:

public ActionResult Create() 
    { 
     var MList = new List<string>(); 
     var Query = from d in db.Dictionaries 
         orderby d.DeviceManufacturer 
         select d.DeviceManufacturer; 
     MList.AddRange(Query.Distinct()); 
     var NList = new List<string>(); 
     Query = from d in db.Dictionaries 
       orderby d.DeviceName 
       select d.DeviceName; 
     NList.AddRange(Query.Distinct()); 
     ViewBag.Manufacturers = new SelectList(MList); 
     ViewBag.Names = new SelectList(NList); 
     return View(); 
    } 

我怎麼填充dropdownlists:

<div class="form-group"> 
     @Html.DropDownList("Manufacturers"); 

    </div> 

    <div class="form-group"> 
     @Html.DropDownList("Names"); 
    </div> 

我的數據庫架構(代碼前):

public class Device 
{ 
    public int DeviceId { get;set; } 
    public string DeviceSerialNumber { get; set; } 
    [Required] 
    [StringLength(14)] 
    public string DeviceUser { get; set; } 
    public int DeviceDictionaryId { get; set; } 
    [ForeignKey("DeviceDictionaryId")] 
    public virtual DeviceDictionary DeviceDictionary {get;set;} 
    public string Batch { get; set; } 
} 
public class DeviceDictionary 
{ 

    public int DeviceDictionaryId { get; set; } 
    [Required] 
    public string DeviceManufacturer { get; set; } 
    [Required] 
    public string DeviceName { get; set; } 

} 

回答

1

如果你想讓事情發生在客戶端,你必須使用JavaScript。

因此,您認爲一種做法是在第一個下拉列表中選擇某項內容時調用某個操作併爲用戶提供「新」頁面。

+0

謝謝。但是,那麼我不應該問一些Ajax問題的數據庫引擎來獲取新數據嗎?我在問,因爲我在網頁設計方面很新穎。 – szpic

+0

您可以使用可用的Ajax助手,但我會咬蘋果並學習一點JS。完成你想要的東西很簡單,你可以使用應該已經包含在你的項目中的jQuery。 – Zache

+0

也許這就是你正在尋找的東西:http://stackoverflow.com/questions/17790944/filtering-subsequent-dropdowns-with-jquery – Zache