Passing models between controllers in MVC4
Posted
by
wtfsven
on Stack Overflow
See other posts from Stack Overflow
or by wtfsven
Published on 2012-11-28T19:28:47Z
Indexed on
2012/12/07
23:05 UTC
Read the original article
Hit count: 196
asp.net-mvc-4
I'm in the middle of my first foray into web development with MVC4, and I've run into a problem that I just know is one of the core issues in web development in general, but can't seem to wrap my mind around it.
So I have two models: Employee
and Company
. Employee
has a Company
, therefore /Employee/Create
contains a field that is either a dropdown list for selecting an existing Company
, or an ActionLink
to /Company/Create
if none exists.
But here's the issue: If a user has to create a Company
, I need to preserve the existing Employee
model across both Views--over to /Company/Create
, then back to /Employee/Create
.
So my first approach was to pass the Employee
model to /Company/Create
with a @Html.ActionLink("Add...", "Create", "Organization", Model , null)
, but then when I get to Company
's Create(Employee emp)
, all of emp
's values are null
.
The second problem is that if I try to come back to /Employee/Create
, I'd be passing back an Employee
, which would resolve to the POST
Action.
Basically, the whole idea is to save the state of the /Employee/Create
page, jump over to /Company/Create
to create a Company
record, then back to /Employee/Create
to finish creating the Employee
.
I know there is a well understood solution to this because , but I can't seem to phase it well enough to get a good Google result out of it.
I've though about using Session, but it seems like doing so would be a bit of a kludge. I'd really like an elegant solution to this.
© Stack Overflow or respective owner