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?
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 .
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.
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.
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 .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With