Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to pass a class object from one controller action to different controller's action.

Sender Action

public class CourseController : Controller
{
[HttpPost]
public ActionResult CreateNewCourse(CourseViewModelBase courseViewModel)
{
   if (ModelState.IsValid)
   {
       // Do some stuff
       return RedirectToAction("CreateNewProject", "Project",
                          new { courseVM = courseViewModel});
   }
   // Bad happened    
   return View("CreateNewCourse", courseViewModel);
}

Receiver Action

public class ProjectController : Controller
{
[HttpGet]
public ActionResult CreateNewProject(CourseViewModelBase courseVM)
{
      // Use CourseVM data and do other stuff
     return View("Create", projectCreateViewModel);
}
}

I am getting data properly in Sender Action and Receiver Action is called properly from the redirect to action call. However courseVM in Receiver Action is null.

I know this is a very old question and had been asked repetitively. But I found that most of the answers suggested to use TempData and were answered in 2008/2009. I believe there would be someway to pass data using RedirectToAction without using TempData. If there is not then I would go with TempData only.

Finding If I pass some simple data e.g. new {id = courseViewModel.CourseDuration} and change the argument in Receiver action to id then id is properly received.

Similar Questions Question 1
Question 2
Question 3
Question 4
Question 5
Question 6, tried to use this one but did not workout
Question 7
Question 8
Question 9
Question 10

Most of the answers in above questions are dated back in 2008/09 and uses tempdata.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

use this

 return RedirectToAction("ActionName", "ControllerName", modelObj);

in your case

 return RedirectToAction("CreateNewProject", "Course", courseViewModel);

You can also use

 TempData 
 ViewBab
share|improve this answer
 
Is there any harm in going with this approach? i.e. Not using TempData or ViewBag –  Rohit Kandhal Jul 12 at 14:29
 
Rohit: I think "RedirectToAction" is the best approch to go. Thanks –  Chandu Jul 12 at 14:31
 
Hey Pawanism I'm having trouble in sending course VM from View (.cshtml) to controller. It is always coming as null –  Rohit Kandhal Jul 15 at 14:48
 
how you are calling action from view or client side. Means its ajax call or using BeginForm in View. check all the arguments are passing properly. –  Chandu Jul 16 at 5:27
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.