Fpanico
Fpanico

Reputation: 49

ASP.net MVC 4 getting user id from one view and using it in another view

I have two views and two controllers. One for clients and the other for their addresses. Clients can have one address. In my model definition for client I allow the addressID of the client to be nullable. When I want to create an address for a client, according to the addressId being null or not i will redirect to either the address's "create" or "details" view. For now I want to just display the clients name when I reach the Create view("creating address for John Doe"). How do i go about doing this? I'm trying to learn MVC slowly and I'm having trouble dealing with the separation of concerns and where to pass data.

Upvotes: 1

Views: 514

Answers (3)

Venkata K. C. Tata
Venkata K. C. Tata

Reputation: 5547

I would recommend you using session for this .. store the username is session("username") and use it in view or in any controller u want ..

Upvotes: 0

dasheddot
dasheddot

Reputation: 2966

Since you are redirecting manually you know the next request for sure. In this case TempData may fit your needs best. If you need the data for more than the next request you should probably use Session or Cookies as already suggested.

Use this to store data for the next request:

TempData["ClientName"] = "John Doe";

And this to get the data out in the next request:

var clientName = TempData["ClientName"];

Please note that TempData stores its data only for the next request. For further info about TempData MSDN is your friend: http://msdn.microsoft.com/en-us/library/dd394711%28v=vs.100%29.aspx

Upvotes: 1

Jorge Alvarado
Jorge Alvarado

Reputation: 2674

Try to put some code for others to help you better, but for now what I can tell you is that you want to keep state across requests, therefore you have many options. You can keep a session state, you can have a querystring appended to your following requests, you can have cookies transporting the id, etc.

Upvotes: 0

Related Questions