How to redirect the user by using Ajax call on specific Controller and action
本问题已经有最佳答案,请猛点这里访问。
下面是我的javascript函数,点击更新按钮进行更新。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function UpdateData() { var obj = { "testData": $("#hdn_EditdsVal").val(), "feature": $("#hdn_EditdsVal").val() }; $.ajax({ url: '@(Url.Action("UpdatePlanFeatVal","SuperAdmin"))', type:"POST", dataType:"json", data: JSON.stringify(obj), contentType:"application/json", success: function (result) { // want to redirect the user using ControllerName/ActionMethod }, error: function (err) { } }); } |
和我的控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public ActionResult UpdatePlanFeatVal(string testData,string feature) { var cmd = (object)null; testData = testData.Trim(); string[] words = testData.Split(':'); XDocument _xdoc = new XDocument(new XElement("Pricing")); foreach (string word in words) { if (!string.IsNullOrEmpty(word)) { string[] wor = word.Split('_'); _xdoc.Root.Add( new XElement("row", new XElement("FeatureId", wor[1]), new XElement("PlanId", wor[2]), new XElement("Unit", wor[3]) )); } } using (StoreProcedureContext sc = new StoreProcedureContext()) { cmd = sc.EditPricing(_xdoc); } return View("ManageSubscriptionPlan"); } |
它没有将我重定向到那个视图,也做了一些谷歌,发现我必须在javascript本身中这样做,并使用
请在Ajax成功时更新您的javascript函数使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function UpdateData() { var testData= $("#hdn_EditdsVal").val(); var feature= $("#hdn_EditdsVal").val(); }; $.ajax({ url: '@(Url.Action("UpdatePlanFeatVal","SuperAdmin"))', type:"POST", dataType:"json", data: { testData: testData, feature: feature }, contentType:"application/json", success: function (result) { window.location.href = '@Url.Action("Action","Controller")'; }, error: function (err) { } }); } |