填充DROPDOWNLIST使用MVC2基于另一个DROPDOWNLIST(层叠的DropDownList) [英] Populating Dropdownlist Using MVC2 Based On Another Dropdownlist (Cascading DropDownList)

查看:95
本文介绍了填充DROPDOWNLIST使用MVC2基于另一个DROPDOWNLIST(层叠的DropDownList)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与车辆交易的应用程序。我需要两个DropDownLists:

I am making an application that deals with vehicles. I need two DropDownLists:


  • 使:所有车辆进行

  • 型号:属于特定型号
    在制作的DropDownList值

这是如何在MVC2做?

我的想法:我使用Ajax调用选择我的第一个列表时,再拉回来的模型绑定到模型DDL?如何将模型绑定发挥作用呀?

My Idea: Do I use an ajax call when my first list is selected and then pull back the Models to bind to the Model DDL? How would model binding come into play that way?

更新我贴我终于实现了作为一个答案。它的超级简单和伟大工程。

UPDATE I posted what I ended up doing as an answer. It is super simple and works great.

您可以用太,如果你觉得这样的倾向,但你必须指定你想是这样的... 返回JSON(citiesList,JsonRequestBehavior.AllowGet);

You can use a get too if you feel so inclined, but you have to specify that you want to like so... return Json(citiesList, JsonRequestBehavior.AllowGet);

推荐答案

这是我落得这样做......没有需要额外的插件/ 1000线code ...

This is what I ended up doing... Didn't need additional plugins / 1000 Lines of code...

//The first DDL is being fed from a List in my ViewModel, You can change this...
<%: Html.DropDownList("MakeList", new SelectList(Model.Makes, "ID", "Name")) %>
<select id="ModelID" name="ModelID" disabled="disabled"></select>

jQuery的

    $(document).ready(function () {
        $('#MakeList').change(function () {
            $.ajaxSetup({ cache: false });
            var selectedItem = $(this).val();
            if (selectedItem == "" || selectedItem == 0) {
                //Do nothing or hide...?
            } else {
                $.post('<%: ResolveUrl("~/Sell/GetModelsByMake/")%>' + $("#MakeList > option:selected").attr("value"), function (data) {
                    var items = "";
                    $.each(data, function (i, data) {
                        items += "<option value='" + data.ID + "'>" + data.Name + "</option>";
                    });
                    $("#ModelID").html(items);
                    $("#ModelID").removeAttr('disabled');
                });
            }
        });
    });

行动

    [HttpPost]
    public ActionResult GetModelsByMake(int id)
    {
        Models.TheDataContext db = new Models.TheDataContext();
        List<Models.Model> models = db.Models.Where(p=>p.MakeID == id).ToList();

        return Json(models);
    }

这篇关于填充DROPDOWNLIST使用MVC2基于另一个DROPDOWNLIST(层叠的DropDownList)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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