如何使用Ajax调用特定的控制器和操作来重定向用户

How to redirect the user by using Ajax call on specific Controller and action

本文关键字:操作 重定向 用户 控制器 Ajax 何使用 调用      更新时间:2023-09-26

下面是我的Javascript函数,它将在单击"更新"按钮时进行更新。

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

还有我的控制器

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 (StoredProcedureContext sc = new StoredProcedureContext())
            {                    
                cmd = sc.EditPricing(_xdoc);             
            }
        return View("ManageSubscriptionPlan");
   }

它没有将我重定向到那个视图,还做了一些谷歌搜索,发现我必须在Javascript中使用这个东西,并使用OnSuccess选项调用url。任何关于如何在我的场景中使用javascript进行回发的想法。

而且也不要在发布之前查看代码,因为它是经过修改的。我只想在更新后进行重定向。

请在Ajax成功后更新您的javascript函数使用。

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