I have a search box that is implemented as a partial view. I would like the last searched value to remain (think of Yelp, where the location is always there, regardless of the page you are viewing). I am thinking of storing this in a cookie, but I am not sure where to add the code to read the cookie and add the value to the view model. Any suggestions would be appreciated.
A:
public static class SiteSettings
{
//private static string _location;
public static string Location
{
get
{
return Response.Cookies["location"].Value;
}
}
private static string _loginReturnUrl;
public static string LoginReturnUrl
{
get
{
if (_loginReturnUrl == null)
_loginReturnUrl = WebConfigurationManager.AppSettings["LoginReturnUrl"];
return _loginReturnUrl;
}
}
}
Update Then in your controller, partial or code-behind you could call it like so...
string location = SiteSettings.Location;
I wouldn't put a location property in any model unless it's a base model that other models that need location information inherit...
I use this pattern to access parameters from different stores. Use it to cache values that don't change often...
Alexander
2010-08-15 21:11:56
I should have mentioned that the partial view is called from the master page. What controller would call this? All of them? Or should this call be in the partial view?Thanks!
Michael Teper
2010-08-15 21:43:00
@Michael, the call(reference) would be in the Partial View's Markup.
Praveen
2010-08-15 22:02:33
Yup, that works well enough. Thanks!
Michael Teper
2010-08-15 22:25:03