Search code examples
jqueryajaxmodel-view-controlleractionresult

redirect to another view with multiple parameter, located in different controller jquery


I am working on mvc and I am stuck at one place where I want to redirect from a view to another,

I have 2 method in my controller where I want to redirect from one to another,

here is my methods,

    public ActionResult Test1()
    {
        return View();
    }
    [HttpGet]
    public ActionResult Rest2()
    {
        return View();
    }

and my ajax call

var url = window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/") + 1);//to get current url
$.ajax({
    url:  "@Url.Action("../RegForm/Rest2")",
    type: "GET",
    contentType: "application/json; charset=utf-8",
    async: true,
    error: function (error) {
    },
    success: function (response) {

    }
});

Debugger is going to my controller but returning on same view

what can I do here?


Solution

  • If you want to redirect, you absolutely do not need AJAX. You use AJAX only when you want to stay on the same page and update only a portion of it.

    So if you only wanted to redirect the browser:

    var url = '@Url.Action("Test2", "Home",new { ID=1, Name="LMK", Date="05-10-18" })'; 
    window.location.href = url