【问题标题】:Cascade DropDownList using Jquery issue使用 Jquery 问题的级联 DropDownList
【发布时间】:2011-05-30 11:30:43
【问题描述】:

所以,我找到了本教程,其中还包含示例的代码源,该示例展示了如何使用 Jquery 制作级联下拉列表。 我尝试将该代码用于我自己的项目示例,但它似乎不起作用。

    public class IndexViewModel
    {
        //1st DDL ID
        public int grupa_id
        {
            get;
            set;
        }


        //1st DropDownList Values
        public List<SelectListItem> GrupeValues
        {
            get;
            set;
        }

        //2nd  DDL ID
        public int produs_id
        {
            get;
            set;
        }


        //2nd DropDownList Values
        public List<SelectListItem> ProduseValues
        {
            get;
            set;
        }
    }

控制器。:

    public ActionResult Blabla()
    {
        DataRepository objRepository = new DataRepository();

        IndexViewModel objIndexViewModel = new IndexViewModel();

        objIndexViewModel.GrupeValues = objRepository.GetGrupa();

        //Get the first item of the First drop down list(State ddl)
        string first = objIndexViewModel.GrupeValues[0].Value;

        //Get the City names based on the first Item in the State ddl
        objIndexViewModel.ProduseValues = objRepository.GetProduse(Convert.ToInt16(first));

        return View(objIndexViewModel);
    }

然后是返回jsonresult的actiont:

    public JsonResult Cities_SelectedState(int param)
    {
        DataRepository objRepository = new DataRepository();

        JsonResult result = new JsonResult();
        var vCities = objRepository.GetProduse(Convert.ToInt16(param));
        result.Data = vCities.ToList();
        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        return result;
    }

观点:

       <script type="text/javascript">
           $(document).ready(function () {
               $("#grupa_id").change(function () {
                   var url = '<%= Url.Content("~/") %>' + "Home/Cities_SelectedState";
                   var ddlsource = "#grupa_id";
                   var ddltarget = "#produs_id";
                   $.getJSON(url, { param: $(ddlsource).val() }, function (data) {
                       $(ddltarget).empty();
                       $.each(data, function (index, optionData) {
                           $(ddltarget).append("<option value='" + optionData.Text + "'>" + optionData.Value + "</option>");
                       });

                   });
               });
           });

   </script>

<p>

  <%:Html.Label("Grupe:") %>
  <%:Html.DropDownListFor(m=>m.grupa_id, Model.GrupeValues) %>

  <%:Html.Label("Produse:") %>
  <%:Html.DropDownListFor(m=>m.produs_id, Model.ProduseValues)%>        
</p>

我在哪里做错了什么?

【问题讨论】:

    标签: jquery asp.net-mvc json drop-down-menu cascadingdropdown


    【解决方案1】:

    您正在呼叫Home/Cities_SelectedState,但我看不到您在哪里传递int param --- 也许这就是您所缺少的。

    【讨论】:

    • $.getJSON(url, { param: $(ddlsource).val() }, 函数(数据)
    • 啊!您是否检查了进入控制器时填充的 RouteData?可能该类型与int 不对应。我已经放弃了参数:)
    • 不知道如何让它在 MVC 2 中工作,但在切换到 MVC 3 后它可以工作。感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多