[英]Cascading Dropdown lists MVC 3 C#
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.如您所见,我有两个相同的 Car_Name,但 Car_drive 不同。 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.因此,当用户单击第一个下拉列表时,他们将看到 Honda、Acura、Toyota...但如果他们是 select Honda,则第二个下拉框将显示 2wheeldrive 和 4wheeldrive。
My Controller looks likes this:我的 Controller 看起来像这样:
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.我接近 MVC Awesome 的东西,但我不断收到错误消息,说 SelectableItem 缺少引用。 Any help on getting this implemented would be great.任何有助于实现这一点的帮助都会很棒。
update I have added this to my controller更新我已将此添加到我的 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.现在需要一些帮助来编写脚本以从 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.检查: http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp.net-mvc.aspx和http://msprogrammer.serviciipeweb.ro/2011/ 02/13/asp.net-mvc-jquery-and-razor-cascading-dropdown-retrieving-partial-views-json-send-objects-handling-errors/你不需要任何其他东西。 Both projects have a demo to download to inspect code.这两个项目都有一个演示可供下载以检查代码。
Regards问候
EDIT: After your update编辑:更新后
Don't use ViewData or ViewBag.不要使用 ViewData 或 ViewBag。 Make a ViewModel containing properties for Cars and CarDrive it's cleaner and easier to add something new.创建一个包含 Cars 和 CarDrive 属性的 ViewModel,它更干净,也更容易添加新内容。
Using some client side jQuery scripting, have your Car_Name
dropdown fire off an event whenever someone changes the selection.使用一些客户端 jQuery 脚本,让您的Car_Name
下拉菜单在有人更改选择时触发一个事件。
//Use your DOM identifier here. I don't know what it's called.
$('#car_name').change(function() {
//Magic here.
});
Now what you want to do is, when this event is fired, fire off an AJAX request with the value of Car_Name
.现在您要做的是,当此事件被触发时,触发一个值为 Car_Name 的Car_Name
请求。
Have your ActionMethod return a JsonResult
.让您的 ActionMethod 返回JsonResult
。
You then catch that information on the client side, and parse the data to insert select options into your dropdown Car_Drive
.然后您在客户端捕获该信息,并解析数据以将 select 选项插入到您的下拉Car_Drive
中。
That's what you need to do, broken down in small pieces that is simple to follow.这就是您需要做的,分解成易于遵循的小块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.