级联下拉列表MVC 3 C# [英] Cascading Dropdown lists MVC 3 C#

查看:207
本文介绍了级联下拉列表MVC 3 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个DropDownList的,我所要做的就是让他们级联。使得第二个是依赖于第一。究其原因是因为我的数据库是奠定了这样的

  **** Car_iD Car_Name Car_drive **
      1本田2轮驱动
      2讴歌4wheel驱动器
      3丰田2轮驱动
      4本田4wheelDrive

正如你可以看到我有两个Car_Names是相同的,但Car_drive是不同的。因此,当用户点击第一个下拉列表中,他们将看到本田,讴歌,丰田......但如果​​他们选择本田第二个下拉框会说2wheeldrive和4wheeldrive。

我的控制器看起来喜欢这样的:

 公众的ActionResult主页()
  {
  计算机[Car_Name] =新的SelectList(_context.Cars.Select(A => a.Car_Name).Distinct());
   计算机[Car_drive] =新的SelectList(_context.Cars.Select(A => a.Car_drive).Distinct());
  }

我的视图看起来像这样

 选择一个汽车
               <标签=Car_Names>< /标签>
                &所述;%= Html.DropDownList(Car_Name)%>               <标签=Application_Names>< /标签>
                &所述;%= Html.DropDownList(Car_drive)%>
                <输入类型=提交值=更新/>
                 <使用(Html.BeginForm(家,家)){%GT%;
                <%}%GT;

我经历了那么多的教程读,但没有真正接近。我接近的MVC真棒的东西,但我一直得到错误说SelectableItem缺少引用。在得到这个实现的任何帮助将是巨大的。

更新
我已经加入这个我控制器

 公众的ActionResult CarNameChange(字符串Car_Name)
    {
        VAR car_drive =从_context.Cars ENV
                              其中,env.Car_Name == Car_Name
                              选择car_drive;
        返回JSON(计算机[Car_Drive] = car_drive);
    }

现在需要一些帮助,写剧本,以获得从控制器此信息。

 <脚本类型=文/ JavaScript的>
   $('#Car_Names')。改变(函数(){
      VAR selectedName = $(本).VAL();
       $的getJSON('@ Url.Action(


解决方案

检查:<一href=\"http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx和<一个href=\"http://msprogrammer.serviciipeweb.ro/2011/02/13/asp-net-mvc-jquery-and-razor-cascading-dropdown-retrieving-partial-views-json-send-objects-handling-errors/\" rel=\"nofollow\">http://msprogrammer.serviciipeweb.ro/2011/02/13/asp-net-mvc-jquery-and-razor-cascading-dropdown-retrieving-partial-views-json-send-objects-handling-errors/你将不再需要任何东西。这两个项目都有一个demo下载到检查code。

问候

编辑:您的更新后

不要使用ViewData的或ViewBag。使包含汽车和CarDrive它的清洁和更容易添加一些新特性的视图模型。

I have created two dropdownlist, what i am trying to do is make them cascade. Such that the second one is dependent on the first. The reason is because my database is laid out like this

****Car_iD      Car_Name            Car_drive** 
      1           Honda             2 wheel drive
      2           Acura             4wheel drive        
      3           Toyota            2 wheel drive
      4           Honda             4wheelDrive       

As you can see I have two Car_Names that are the same but the Car_drive is different. So when the user clicks on the first dropdown list they will see Honda, Acura, Toyota...but if they select Honda the second dropdown box would say 2wheeldrive and 4wheeldrive.

My Controller looks likes this:

 public ActionResult Home()
  {
  ViewData["Car_Name"] = new SelectList(_context.Cars.Select(a => a.Car_Name).Distinct());
   ViewData["Car_drive"] = new SelectList(_context.Cars.Select(a => a.Car_drive).Distinct());
  }

My View looks like this

      Choose an Car
               <label for= "Car_Names"></label>
                <%= Html.DropDownList("Car_Name" )%>  

               <label for= "Application_Names"></label>
                <%= Html.DropDownList("Car_drive")%>    
                <input type = "submit" value ="Update" /> 
                 <% using(Html.BeginForm("Home", "Home")) { %>  
                <%}%>       

I have read through so many tutorial but nothing really comes close. I got close on the MVC Awesome stuff but I kept getting errors saying SelectableItem is missing a references. Any help on getting this implemented would be great.

update I have added this to my controller

    public ActionResult CarNameChange(string Car_Name)
    {
        var car_drive = from env in _context.Cars
                              where env.Car_Name == Car_Name
                              select car_drive;
        return Json(ViewData["Car_Drive"] = car_drive); 
    }

Now need some help writing the script to get this information from the controller.

   <script type = "text/javascript">
   $('#Car_Names').change(function(){
      var selectedName = $(this).val();
       $getJson('@Url.Action("

解决方案

Check: http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx and http://msprogrammer.serviciipeweb.ro/2011/02/13/asp-net-mvc-jquery-and-razor-cascading-dropdown-retrieving-partial-views-json-send-objects-handling-errors/ and you won't need anything else. Both projects have a demo to download to inspect code.

Regards

EDIT: After your update

Don't use ViewData or ViewBag. Make a ViewModel containing properties for Cars and CarDrive it's cleaner and easier to add something new.

这篇关于级联下拉列表MVC 3 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆