Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we pass model as a parameter in RedirectToAction?

I want to know, there is any technique so we can pass Model as a parameter in RedirectToAction

For Example:

public class Student{     public int Id{get;set;}     public string Name{get;set;} } 

Controller

public class StudentController : Controller {     public ActionResult FillStudent()     {         return View();     }     [HttpPost]     public ActionResult FillStudent(Student student1)     {         return RedirectToAction("GetStudent","Student",new{student=student1});     }     public ActionResult GetStudent(Student student)     {         return View();     } } 

My Question - Can I pass student model in RedirectToAction?

like image 346
Amit Avatar asked Mar 19 '14 12:03

Amit


People also ask

Can we pass model in RedirectToAction?

My Question - Can I pass student model in RedirectToAction? Since the route dictionary deals with objects, try changing the GetStudent action to accept an object and inside cast it to Student . Another option would be to serialize it using JSON when passing it from FillStudent .

Can we pass model to RedirectToAction in MVC?

When a Button is clicked, the Model object is populated with values and passed to the RedirectToAction method along with the name of the Controller and its Action method in ASP.Net MVC Razor.

Is it possible to pass a model object to a view from a controller?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

What is difference between RedirectToAction and RedirectToRoute?

RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .


1 Answers

Using TempData

Represents a set of data that persists only from one request to the next

[HttpPost] public ActionResult FillStudent(Student student1) {     TempData["student"]= new Student();     return RedirectToAction("GetStudent","Student"); }  [HttpGet] public ActionResult GetStudent(Student passedStd) {     Student std=(Student)TempData["student"];     return View(); } 

Alternative way Pass the data using Query string

return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"}); 

This will generate a GET Request like Student/GetStudent?Name=John & Class=clsz

Ensure the method you want to redirect to is decorated with [HttpGet] as the above RedirectToAction will issue GET Request with http status code 302 Found (common way of performing url redirect)

like image 194
Murali Murugesan Avatar answered Sep 22 '22 16:09

Murali Murugesan