James Hay
James Hay

Reputation: 12700

How do I persist a value accross views

Completely new to asp.net mvc... completely new to web apps so bear with me...

Lets say I have an Action on a controller that requires a specific piece of information, say an int Id value.

A view is rendered from this Action. This view contains a button which will take the user off to a new Action on another Controller.

On the view of this second Action, there is a link that will send them back to the original Action on the Controller. Obviously, I've lost the original Id value and therefore the information I need to render my original view. What do I need to be looking into to solve this? Are there techniques/patterns that could be used to help?

I know I can keep passing the value around but if the other controller doesn't actually need this value it seems wasteful. I think I'm probably approaching the problem the wrong way to be honest. Any help appreciated.

Upvotes: 0

Views: 81

Answers (2)

Çağdaş Tekin
Çağdaş Tekin

Reputation: 16651

There are many ways you can achieve this. Some of them are :

  • You can keep the value in the Query String.
  • If navigation from Action-1 to Action-2 is done via posting a form, you can send the value with a hidden input tag. Eg : <input type="hidden" name="n" value="v" />
  • You can use TempData to persist information between two requests (though it doesn't look like this is a good solution for your case)

Without knowing what exactly you are trying to do, it's hard to know best solution.

Upvotes: 0

Jeff Sternal
Jeff Sternal

Reputation: 48593

I know I can keep passing the value around but if the other controller doesn't actually need this value it seems wasteful.

But the second controller does need the id to function properly - as you've said, it needs the id to render the return link. Passing it around is the right thing to do.

Upvotes: 3

Related Questions