简体   繁体   English

MVC4:如何传递嵌套的Ajax请求?

[英]MVC4 : How to pass nested Ajax request?

I make form in a web application , that i filter data according to user selection with ajax and get data with jason all worked well but the problem occur when i want to get nested data that i get from table BankPlans value and for each bankid i get all required docs from other table i try to get them in one method in jason to select 我在Web应用程序中形成表单,即我根据用户选择的ajax筛选数据并使用jason获取数据的方法都工作良好,但是当我想获取从表BankPlans值中获取的嵌套数据以及我获取的每个bankid时出现问题来自其他表的所有必需文档,我尝试在jason中以一种方法获取它们以进行选择

new { Percent = r.percent, Reqdocs = docs });

and in the result of ajax loop for each reqDosc to put them in list 并在每个reqDosc的ajax循环中将它们放入列表中

  • i get error when execute method in controller say case 1 = operation and case 2 = operation then i try another solution to pass them in two methods one to get the percent and id and pass them to the first ajax and for each id get the required docs the first method execute well but the second get the docs for the last id and put them in all plans when i make abreak point in the next mehod it actually pass in the paramters the three values and only one to execute i think it need async between them but i cannot to do it or if any other solution to solve the problem. 当控制器中的execute方法说情况1 =操作和情况2 =操作时,我得到了错误,然后我尝试另一种解决方案,以两种方法传递它们,一个获得百分比和id,然后将它们传递给第一个ajax,对于每个id获得所需的值docs第一种方法执行得很好,但是第二种方法获得了最后一个id的文档,当我在下一个方法中设定断点时将它们放入所有计划中,它实际上将参数传递给这三个值,只有一个要执行,我认为它需要异步他们之间,但我不能做到这一点,或者如果有任何其他解决方案来解决问题。

    Ajax : 阿贾克斯:

      var addressData = JSON.stringify(jsonData); $.ajax({ url: '/Getyourcar/GetPlans', type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", data: addressData, success: function (result) { $("#carousel").empty(); for (i = 0; i < result.length; i++) { $("#carousel").append("<div class='carousel-feature'><div class='content'><h3>الفائدة السنوية</h3><div class='rate'> " + result[i].percent + " </div><div class='feature_cnt'> <ul></ul> </div> <a href='javascript:void(0)' class='check' title=" + result[i].percent + ">اختر</a></div><img class='carousel-image' alt='Image Caption' src='/images/1px.png'></div>"); $('input[name="planid"]').val(result[i].planid); getDocs(); } }, error: function (result) { } }); } var getDocs = function () { var jsonData = { "Planid": $("#planid").val(), }; var addressData = JSON.stringify(jsonData); $.ajax({ url: '/Getyourcar/Getdocs', type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", data: addressData, success: function (result) { $("#carousel .feature_cnt ul").empty(); for (i = 0; i < result.length; i++) { $("#carousel .feature_cnt ul").append("<li>" + result[i].plan + " </li>"); } }, error: function (result) { alert("failed"); } }); 

    Controller : 控制器:

      public ActionResult GetPlans(string BankName = "", string Period = "", string customergrade = "") { int bankid = db.Banks.Where(b => b.Name.Contains(BankName) || BankName == "").FirstOrDefault().Id; int yearid = db.Yearss.Where(b => b.Name.Contains(Period) || Period == "").FirstOrDefault().Id; int cusgradeid = db.CustomerGrades.Where(c => c.Name.Contains(customergrade) || customergrade == "").FirstOrDefault().Id; var Bankplanid = db.BankPlanss.Where(b => (b.BankId == bankid) && (b.CustomerGradeId == cusgradeid)) .Select(r => new { cusgrade = cusgradeid, bankid = r.BankId, percent = r.Percent, planid = r.Id }); return Json(Bankplanid, JsonRequestBehavior.AllowGet); } public ActionResult Getdocs(int Planid = 0) { var plans = db.BankPlansReqDocs.Where(b => b.BankPlansId == Planid) .Select(r => new { plan = r.RequiredDocs.Name }); return Json(plans, JsonRequestBehavior.AllowGet); } 

    When use debugging in VS, case of the Getdocs method called twice in the first time the Planid in the parameter get the value when press F10 the same line called again with the next value and return the Jason of the last value. 在VS中使用调试时,在第一次按两次调用带有下一个值的同一行并返回下一个值的Jason并返回上一个值的Jason的情况下,在参数中的Planid首次获得两次的Getdocs方法的情况下。

    Please help , Thanks in advance . 请帮忙,谢谢。

  • The problem solved by pass all docs as a string separated with( , )and all ids in a list . 通过将所有文档作为以(,)和list中的所有ID分隔的字符串传递来解决该问题。

    • In java script split(",") and insert them in specified ul id 在Java脚本split(“,”)中,并将其插入指定的ul id中

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM