0

Below is my Javascript function which will do update on the click of Update button.

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

        }
    });
}

And my controller

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

It is not redirecting me to that view and also did some google and found that i have to this thing in Javascript itself and call the url using OnSuccess option. Any idea how to do postback by using javascript in my scenario.

And also don't go through the code as it is modified before posting. I just want redirect to be happen after update.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Steve
  • 352
  • 2
  • 6
  • 24
  • The whole purpose of ajax is to stay on the same page, and using ajax to post data when you want to redirect is just pointless. Do a normal submit and save yourself some code and improve performance. –  May 17 '16 at 12:06

1 Answers1

2

Please update your javascript function use on success of Ajax.

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

    }
});
}
Banwari Yadav
  • 506
  • 1
  • 3
  • 18
  • Thank you for your input.i got my answer but still marking this as answer because of no other option :P – Steve May 17 '16 at 15:42