如何使用 JSON 返回适当的结果以填充级联下拉列表?

发布于 2024-11-29 20:03:09 字数 1813 浏览 2 评论 0原文

我正在尝试根据第一个下拉列表(包含国家/地区)中选择的内容来填充第二个下拉列表(其中包含县)。

我有一个名为 GetCounties 的控制器操作:

  public JsonResult GetCounties(string id)
    {
        DBEntities dc = new DBEntities();
        JsonResult result = new JsonResult();
        var filteredDivisions = from div in dc.Divisions
                                where div.CountryID == 1 //id
                                select div;
        result.Data = filteredDivisions.ToList();        
        return Json(result.Data, JsonRequestBehavior.AllowGet);          

        //         return Json(new[] {
        //    new { Id = filteredDivisions.ToArray()[0].DivisionID, Value = filteredDivisions.ToArray()[0].DivisionName },
        //    new { Id = 2, Value = "value 2" },
        //    new { Id = 3, Value = "value 3" },
        //}, JsonRequestBehavior.AllowGet);
    }

请注意被注释掉的部分。这对我来说效果很好,(我从分区列表中获取第一个值,另外两个值称为“值 2”和“值 3”。我用它来测试回调,但这是唯一给我的东西结果:

 return Json(result.Data, JsonRequestBehavior.AllowGet);     

在调试时,所有正确的值都存在,但它们不会出现在视图端的下拉列表中,正如我所说,我得到的唯一结果是。使用注释掉的代码(硬编码值,这当然不够好)我尝试使用 for 循环来迭代列表以填充数组,但没有任何效果,我收到诸如匿名类型只读之类的错误,因此我无法再次设置它。

这是我的jquery代码:

  $(function () {
            $.getJSON('/Entity/GetCounties', function (result) {
                var ddl = $('#DivisionsList');
                ddl.empty();
                $(result).each(function () {
                    $(document.createElement('option'))
            .attr('value', this.Id)
            .text(this.Value)
            .appendTo(ddl);
                });
            });
        });

divisionslist当然是包含所有县的下拉列表,通过使用上面的非注释返回语句,仅返回不变,即包含其所有值,而不仅仅是应该出现的值在选定的 国家。 那么,如何使用过滤后的结果填充数组,以便可以使用 JSON 将其返回到视图?我想使用我发布的这段代码,因为它是唯一有效的代码。我只需要将硬编码值替换为正确的值即可。这就是我遇到麻烦的地方。

请帮我。

I'm trying to populate a second drop down list (which contains counties) depending on what is selected on the first drop down list (which contains countries).

I have a controller action called GetCounties:

  public JsonResult GetCounties(string id)
    {
        DBEntities dc = new DBEntities();
        JsonResult result = new JsonResult();
        var filteredDivisions = from div in dc.Divisions
                                where div.CountryID == 1 //id
                                select div;
        result.Data = filteredDivisions.ToList();        
        return Json(result.Data, JsonRequestBehavior.AllowGet);          

        //         return Json(new[] {
        //    new { Id = filteredDivisions.ToArray()[0].DivisionID, Value = filteredDivisions.ToArray()[0].DivisionName },
        //    new { Id = 2, Value = "value 2" },
        //    new { Id = 3, Value = "value 3" },
        //}, JsonRequestBehavior.AllowGet);
    }

Notice the section that is commented out. That works fine for me, (I get the first value from the divisions list, and the other two called "value 2" and "value 3". I used that there to test the callback, but it was the only thing that gave me results. Using:

 return Json(result.Data, JsonRequestBehavior.AllowGet);     

doesn't achieve anything on the view side. When debugging, all the right values are there, but they don't come up on the view side's drop down list. As I said, the only results I get is by using the code that is commented out (the hardcoded values, which of course are not good enough). I tried to use for loops to iterate through the list in order to populate the array, but nothing works, I get errors like anonymous type is read only so I cannot set it again.

This is my jquery code:

  $(function () {
            $.getJSON('/Entity/GetCounties', function (result) {
                var ddl = $('#DivisionsList');
                ddl.empty();
                $(result).each(function () {
                    $(document.createElement('option'))
            .attr('value', this.Id)
            .text(this.Value)
            .appendTo(ddl);
                });
            });
        });

The divisionslist is of course the dropdown with all the counties, which by using the non-commented return statement above, just returns unchanged, i.e. containing ALL of its values, and not just the ones that should come up depending on the selected country.
so please HOW CAN I POPULATE AN ARRAY WITH THE FILTERED RESULTS SO THAT I CAN RETURN IT TO THE VIEW USING JSON? I want to use THIS code that I have posted, as it was THE ONLY ONE that ever worked. I just need to swap the hardcoded values with the right ones. that's where i'm having trouble.

please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痞味浪人 2024-12-06 20:03:09
$.getJSON('/Entity/GetCounties', {id: $('#yourCountrySelect').val()},function (result) {

现在我会读你的文字墙^^

$.getJSON('/Entity/GetCounties', {id: $('#yourCountrySelect').val()},function (result) {

and now I will read your wall of text ^^

梦萦几度 2024-12-06 20:03:09

我自己解决了。我所做的是这样的:

   public JsonResult GetDivisions(int id)
    {
        ASNEntities dc = new ASNEntities();
        JsonResult result = new JsonResult();
        var filteredDivisions = from div in dc.Divisions
                                where div.CountryID == id
                                select div;

        List<object> listToReturn = new List<object>();

        for (int i = 0; i < filteredDivisions.Count(); i++)
        {
            Object[] obj = new[]{
             new { Id = filteredDivisions.ToArray()[i].DivisionID, Value = filteredDivisions.ToArray()[i].DivisionName },
        };

           listToReturn.Add(obj[0]);              
        }

        return Json(listToReturn.ToArray(), JsonRequestBehavior.AllowGet);

        //return Json(new[] {
        //    new { Id = filteredDivisions.ToArray()[0].DivisionID, Value = filteredDivisions.ToArray()[0].DivisionName },
        //    new { Id = 2, Value = "value 2" },
        //    new { Id = 3, Value = "value 3" },
        //}, JsonRequestBehavior.AllowGet);

    }

效果很好。

I solved it myself. What i did is this:

   public JsonResult GetDivisions(int id)
    {
        ASNEntities dc = new ASNEntities();
        JsonResult result = new JsonResult();
        var filteredDivisions = from div in dc.Divisions
                                where div.CountryID == id
                                select div;

        List<object> listToReturn = new List<object>();

        for (int i = 0; i < filteredDivisions.Count(); i++)
        {
            Object[] obj = new[]{
             new { Id = filteredDivisions.ToArray()[i].DivisionID, Value = filteredDivisions.ToArray()[i].DivisionName },
        };

           listToReturn.Add(obj[0]);              
        }

        return Json(listToReturn.ToArray(), JsonRequestBehavior.AllowGet);

        //return Json(new[] {
        //    new { Id = filteredDivisions.ToArray()[0].DivisionID, Value = filteredDivisions.ToArray()[0].DivisionName },
        //    new { Id = 2, Value = "value 2" },
        //    new { Id = 3, Value = "value 3" },
        //}, JsonRequestBehavior.AllowGet);

    }

and it works great.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文