开发者

How to persist a value in MVC partial view?

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 开发者_如何学Goin 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.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜