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?
Just call the action no need for
redirect to action
or thenew
keyword for model.Using TempData
Alternative way Pass the data using Query string
This will generate a GET Request like
Student/GetStudent?Name=John & Class=clsz
i did find something like this, helps get rid of hardcoded tempdata tags
Yes you can pass the model that you have shown using
assuming
student1
is an instance ofStudent
which will generate the following url (assuming your using the default routes and the value of
student1
areID=4
andName="Amit"
).../Student/GetStudent/4?Name=Amit
Internally the
RedirectToAction()
method builds aRouteValueDictionary
by using the.ToString()
value of each property in the model. However, binding will only work if all the properties in the model are simple properties and it fails if any properties are complex objects or collections because the method does not use recursion. If for example,Student
contained a propertyList<string> Subjects
, then that property would result in a query string value of....&Subjects=System.Collections.Generic.List'1[System.String]
and binding would fail and that property would be
null