使用Jquery发出级联DropDownList

问题描述

| 因此,我发现本教程还有示例的代码源,该示例显示了如何使用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的动作:
    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>
我在哪里和哪里做错了?     

解决方法

        您正在呼叫
Home/Cities_SelectedState
,但我看不到您将
int param
传递到的位置---也许那是您所缺少的。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...