[英]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循环中将它们放入列表中
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分隔的字符串传递来解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.