.NET Core MVC Can Ajax Redirect Page with Model

I am posting my data from front end using ajax to the backend controller, what I am expecting is that the controller generate a model and redirect a new View with the model; but it never gets redirected. Is it possible to redirect inside the controller? Not Ajax?

I have looked some other threads saying the redirect code need to be written in the Ajax Success function, which it works but I am not able to pass the model to the new view. Is there any way to pass a model alone with ajax redirect? (I cannot pass my data through URL because the amount of data is kind of huge). Please advice, Thank you in advance.

Controller Method:

public IActionResult Input([FromBody] IEnumerable<HistTable> Input) {     //Some Code     return View("/views/test/test.cshtml", model); } 

Ajax:

$.ajax({                 type: "POST",                 url: "/test/input",                 dataType: "json",                 data: JSON.stringify(data),                 contentType: "application/json; charset=utf-8",                 success: function (e) {                     //How to pass the model?                     //window.location.replace(link);                 }             }); 
Add Comment
2 Answer(s)

Is it possible to redirect inside the controller? Not Ajax?

In fact, if ajax is completely aside, the real redirect operation cannot be realized only inside the controller.

You should achieve it combine ajax and controller by these two ways: TempData or Session to achieve this function.

And note that since you are passing multiple pieces of data, please serialize them into json before storing, and get them through deserialization.

1.Session

In this way, pass the data through the session in the controller, and then redirect to the test view in success function in ajax.

First enable Session in the startup.cs file of the core project.

Then, use the following code:

   public IActionResult Test(List<Employee> model)         {             if (HttpContext.Session.GetString("model") != null)             {                 model = System.Text.Json.JsonSerializer.Deserialize<List<Employee>>(HttpContext.Session.GetString("model"));             }              return View(model);         }         public IActionResult Input([FromBody] List<Employee> Input)         {               //Some Code             HttpContext.Session.SetString("model", System.Text.Json.JsonSerializer.Serialize(Input));              return Content("/test/test");         } 

Ajax:

 $.ajax({                 type: "POST",                 url: "/Test/input",                 //dataType: "json",                 data: JSON.stringify(                     [{ Id: 1, FirstName: 'a', LastName: 'v', Gender: 'male' },                     { Id: 2, FirstName: 'b', LastName: 'v', Gender: 'female' },                     { Id: 3, FirstName: 'c', LastName: 'v', Gender: 'male' }]),                 contentType: "application/json; charset=utf-8",                 success: function (e) {                     window.location.replace(e);                 }             }); 

2.TempData

First, you need to pass model by TempData["data"] from Input to Test action by RedirectToAction("test") , because you are passing multiple data.

public IActionResult Test(List<Employee> model)         {               if (TempData["data"] != null)             {                 model = System.Text.Json.JsonSerializer.Deserialize<List<Employee>>(TempData["data"].ToString());                  TempData.Keep();             }             return View(model);         }         public IActionResult Input([FromBody] List<Employee> Input)         {             //Some Code              TempData["data"] = System.Text.Json.JsonSerializer.Serialize(Input);              return RedirectToAction("test");          }   

Ajax:

$.ajax({                 type: "POST",                 url: "/Test/input",                 //dataType: "json",                 data: JSON.stringify(                     [{ Id: 1, FirstName: 'a', LastName: 'v', Gender: 'male' },                     { Id: 2, FirstName: 'b', LastName: 'v', Gender: 'female' },                     { Id: 3, FirstName: 'c', LastName: 'v', Gender: 'male' }]),                 contentType: "application/json; charset=utf-8",                 success: function (e) {                     window.location.replace("/test/test");                 }             }); 

Here is the test result:

enter image description here

Answered on August 31, 2020.
Add Comment

You’d normally send it as a POST via the body. Or another option is to send some sort of identifier over in the url and use that to do a get of the model on the other side.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.