如何在不加载视图的情况下重新加载/刷新局部视图中的级联下拉列表

问题描述

我在互联网上搜索过这个,但找不到任何合适的答案。 我有一个局部视图,其中有三个不同的下拉列表。我在其中使用了两种不同的功能..

  1. 在更改第一个下拉列表时,其他两个会更新
  2. 在更改第二个下拉列表时,第三个更新

在更改第二个下拉列表之前一切正常,如果我想再次更改第一个下拉列表,第三个下拉列表不会加载并且不返回任何值。

 $("#FoodItemID").change(function () {  //
            var id = $("select#FoodItemID").val();
            $.ajax({
                url: '/FoodRecipe/GetServingType',type: "POST",data: { FoodItemID: $("select#FoodItemID").val() },success: function (result) {
     
                    $("#ServingTypeID").val(result[0]);
                    $("#ServingSubTypeID").val(result[1]);

                }
            });

        });

        $("#ServingTypeID").change(function () {
            $.ajax({
                url: '/FoodRecipe/GetServing',data: { ServingTypeID: $("select#ServingTypeID").val() },success: function (result) {

                    var len = result.length;

                    $("#ServingSubTypeID").empty();
                    for (var i = 0; i < len; i++) {
                        var id = result[i]['id'];
                        var name = result[i]['name'];

                        $("#ServingSubTypeID").append("<option value='" + id + "'>" + name + "</option>");
                        $('#ServingSubTypeID').trigger('select:updated'); //this is not working for me
                    }
                }

            });
        });

这是我的下拉菜单代码。 我无法上传整个部分视图,因为它会导致加载后丢失所有值,我理解我遇到的问题与更改第二个下拉列表后未在下拉列表中更新的值有关(因为值来自控制器更改)

所以我认为一个不错的选择是以某种方式重新加载第三个下拉列表中的值。 但我不知道该怎么做..!!

我知道这可能是最简单的事情,但我无法弄清楚。

解决方法

在这上面花了 17 个小时之后,我已经知道该怎么做了。这是我为将来会像我一样困惑的读者所做的事情。

我回到我的控制器并加载所有项目以及与我想传递的特定 id 相关的特定项目,并为/获取项目。数据绑定了所有项目加载器,是的,它起作用了。并且所有上述脚本都保持不变。

这是我在控制器中所做的更改。

以前的控制器代码

 [HttpPost]               //Getting Sub Type and Serving Type on Food Item ID
    public async Task<IActionResult> GetServingType(int? FoodItemID)
    {
        vmControllerMessage.APIExecutionBaseURL = APIUrls.APIApplicationPath;
        vmControllerMessage.APIExecutionURL = "APIFoodRecipe/GetServingTypes";
        vmControllerMessage.VMControllerObject = FoodItemID;
        HttpResponseMessage httpResponseMessage = PutJsonModelAPIWithNoRedirect();
        if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
        {
            int[] i = JsonConvert.DeserializeObject<int[]>(vmServiceMessage.VMObject.ToString());
            return Json(i);
        }
        else
        {
            return Json(vmServiceMessage);
        }

    }

新代码

 [HttpPost]               //Getting Sub Type and Serving Type on Food Item ID
    public async Task<IActionResult> GetServingType(int FoodItemID)
    {
        VMFoodRecipeDetail model = new VMFoodRecipeDetail();
        model.FoodItemID = FoodItemID;
        vmControllerMessage.APIExecutionBaseURL = APIUrls.APIApplicationPath;
        vmControllerMessage.APIExecutionURL = "APIFoodRecipe/GetServingTypes";
        vmControllerMessage.VMControllerObject = model;
        HttpResponseMessage httpResponseMessage = PutJsonModelAPIWithNoRedirect();
        if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
        {
            VMFoodRecipeDetail obj = JsonConvert.DeserializeObject<VMFoodRecipeDetail>(vmServiceMessage.VMObject.ToString());
            ViewBag.ServingTypeID = new SelectList(obj.ServingTypes,"ID","Name",obj.ServingTypeID);
            ViewBag.ServingSubTypeID = new SelectList(obj.ServingSubTypes,obj.ServingSubTypeID);
            int[] ids = { obj.ServingTypeID,obj.ServingSubTypeID };
            return Json(ids);
        }
        else
        {
            return Json(vmServiceMessage);
        }

    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...