2014-01-17 51 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