Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:46:22+00:00 2026-05-27T14:46:22+00:00

Passing data from a controller to another controller. this is what i am doing,

  • 0

Passing data from a controller to another controller. this is what i am doing, but I dont think it is the correct way of doing it, plz help me modify the code for it to work, share example/tutorial..

e.g

I am using Membership API to create a user account

public ActionResult Register() { return View(); }

[HttpPost]
public ActionResult Register(RegisterModel model)
{
  //creates an account and redirect to CompanyController
  //Also I want to store the userId and pass it to the next controller, I am using a session, ok? 
  Session["userObject"] = userIdGenerated()
  return RedirectToAction("Create", "Company");          
}

CompanyController:

public ActionResult Create() { return View(); }

[HttpPost]
public ActionResult Create(CompanyInformation companyinformation)
{
   //creating company account and I need to store the userid to the company table retrieving from a session
   companyinformation.UserID = Session["userObject"].ToString();
   db.CompanyInformation.Add(companyinformation);
   db.SaveChanges();

   //retrieving companyId that was generated and need to pass to the next controller I tried to use "TempData["companyId"] = companyinformation.CompanyInformationID" But the data is no longer found on httpPost

return RedirectToAction("Create", "Contact");

}

Contact controller

  public ActionResult Create()
  {
   //I tried using ViewBag to store the data from TempDate but the data is no longer found on httpPost
        ViewBag.companyId = TempData["companyId"].ToString();
        return View();
   }

 [HttpPost]
 public ActionResult Create(CompanyContact companycontact)
 {
     companycontact.CompanyInformationID = ???? How do I get the companyId?
     db.CompanyContacts.Add(companycontact);
     db.SaveChanges();
    //Redirect to the next controller...
 }

I hope it is clear what I am trying to do. Maybe use ViewModels but I am not sure how to put it together… Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:46:22+00:00Added an answer on May 27, 2026 at 2:46 pm

    You can pass your UserID parameter directly to your controller’s method as it was a standard navigation flow

    RedirectToAction has one overload that allows you to set routeValues.

      return RedirectToAction("Create", "Company", new { id = userIdGenerated() });    
    

    And in your CompanyController

    public ActionResult Create(int id) { return View(id); }
    

    Since you will have your id in the URL, then you would be able to catch it in your post as well:

    [HttpPost]
    public ActionResult Create(int id, CompanyInformation companyinformation)
    

    Or you can persist it into the model CompanyInformation on GET Create

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Passing small amount of data from one controller to another controller, do I use
Passing data (simple string) from Controller to View I have in my controller: public
AFAIK, there are two ways of passing data to the view from controller :
My question is about passing data from kernel to a user space program. I
I'm reading XML data from the HttpServletRequest in my servlets doPost() and passing the
I'm parsing data from this RSS feed: http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=SAM|AR|AR005|MONTE%20MAIZ , The data there updates once
I'm passing data between my controller and my view using a ViewModel class. When
There seem to be multiple means of passing model data from controllers in asp.net
i m suffering with Passing values from javascript function to controller in cakephp and
In my MVC application I have a problem with passing data from view to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.