2

I'm pretty new to MVC although very familiar with Webforms, its quite a shift and I'm still learning, there is one problem that I have come across that I am having difficulty finding a good answer for.

Suppose in my layout I have a dropdown box for selecting country. I then have a view that displays product details such as stock, item price, distributor etc... All of which will be dependent on the country you have selected. There are other views that also display information dependent on country so I would like to keep the dropdown in my layout and have its selected value persist.

Is the best way to persist the selected country and use that value in controllers through storing it in session, or is there a better approach that I may be missing?

2
  • Session would be a good way. Commented Apr 29, 2015 at 13:33
  • There are several approaches. Yes one being session. Another would be to use a hidden element and post that value between pages. Another would be to use a caching framework. ViewBag would be another option. Or even TempData. But the lifespan on those objects would need to be inline with what your wanting. Commented Apr 29, 2015 at 13:40

2 Answers 2

2

The approach from Web-Forms is quite a bit different. However, Model View Controller is designed to mimic the stateless nature of the Hypertext Transfer Protocol. This creates an incredible amount of flexibility, as each View will embody the exact state that you create.

You have several options:

  • Hidden
  • Session
  • ViewBag
  • Temporary Data

The approach you take will vary based on your context, which is sadly missing. Each will have a severe draw back, one hindrance would be the life cycle. Be wary of the Session, as they can quickly become orphaned bogging the application down.

Once we have more context, a better answer will ensue. Persist through your entire application, or persist through one page with several page states?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Yes I should have said, persist through the entire application.
I would either use some form of Cache or a Session. Sounds like it won't be orphaned, only draw back for a session is the twenty minute storage limit.
Thanks, I wasn't sure if there was some other approach I may have been missing, following your answer I have done some more reading on ViewBag and ViewData and for this requirement session does seem to make the most sense. I'm not sure the 20 minute limit will be too much of a problem.
-1

I think using cookies will be a better option for you.

you can save the selected country in a cookies and fetch the value from the cookie whenever needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.