English 中文(简体)
• 如何将适当的结果归还给JSON,以便把 lists倒名单 populat倒下来?
原标题:How to return the appropriate results with JSON in order to populate cascading drop down lists?

我试图根据第一个下级名单(包括国家)所选择的内容,把第二个下级名单(包括各州)列入。

我有一个称为“承诺”的控制者行动:

  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);     

不能取得任何意见。 当瓦解时,所有正确的价值观都存在,但是,它们并没有出现在意见方的名单上。 正如我所说的那样,我取得的唯一成果是使用所评论的法典(硬编码价值,当然这些价值还不够)。 我试图通过名单来播音,以便填满阵列,但没有任何工作,我只读匿名类型等错误,因此我无法再次确定。

这是我的支部法典:

  $(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.

请帮助我。

最佳回答

我解决了这个问题。 事实是:

   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);

    }

它发挥了巨大作用。

问题回答
$.getJSON( /Entity/GetCounties , {id: $( #yourCountrySelect ).val()},function (result) {

我现在宣读你的文本 wall。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签