ASP.NET MVC:城市列表并将所选城市存储在 cookie 中

发布于 2024-09-24 00:47:48 字数 1332 浏览 11 评论 0原文

我的网站上有城市列表,该列表放置在 Site.Master 中,如下所示:

<a id="<%= selectedCity.CityId %>"><%= selectedCity.Name %></a>
<ul>
    ...
    <li id="<%= city.CityId %>" >
    <%= Html.ActionLink(city.Name,"ChangeCity",new{ newCityId = city.CityId })%>
    </li>
    ...
</ul>

接下来,我的所有控制器都基于 BaseController,它包含下一个代码:

public int CityId {get;set;}
protected override void OnActionExecuting(ActionExecutingContext filterContext){
    if (filterContext.ActionDescriptor.ActionName.ToLower() != "changecity"){
        this.CityId = Convert.ToInt32(Request.Cookies["SiteCityId"]);  
        var city = LoadCity(this.CityId);
        var cities = LoadAllCities(); 
        CitiesModel model = new CitiesModel() { selectedCity=city, allCities=cities};
        ViewData["citiesModel"] = city;
    }
    else{
       Response.Cookies["SiteCityId"]=filterContext.ActionParameters["newCityId"];
    }   
}

在我的所有控制器中,我添加了下一个操作:

[HttpGet]
public ActionResult ChangeCity(string newCityId)
{
    return RedirectToAction(this.MyDefaultAction);
}

主要问题:此架构不太好的工作。在 IE8 中,有时我无法更改当前城市使用链接,如下所示:

http://www.mysite.com/home/changecity/?newCityId=3

您对这个架构有何看法?您可以使用其他方法来创建功能吗?

I have list of cities on my site, this list placed in Site.Master and look like:

<a id="<%= selectedCity.CityId %>"><%= selectedCity.Name %></a>
<ul>
    ...
    <li id="<%= city.CityId %>" >
    <%= Html.ActionLink(city.Name,"ChangeCity",new{ newCityId = city.CityId })%>
    </li>
    ...
</ul>

Next, all my controller are based from BaseController, it contain next code:

public int CityId {get;set;}
protected override void OnActionExecuting(ActionExecutingContext filterContext){
    if (filterContext.ActionDescriptor.ActionName.ToLower() != "changecity"){
        this.CityId = Convert.ToInt32(Request.Cookies["SiteCityId"]);  
        var city = LoadCity(this.CityId);
        var cities = LoadAllCities(); 
        CitiesModel model = new CitiesModel() { selectedCity=city, allCities=cities};
        ViewData["citiesModel"] = city;
    }
    else{
       Response.Cookies["SiteCityId"]=filterContext.ActionParameters["newCityId"];
    }   
}

In all my controllers I was add next action:

[HttpGet]
public ActionResult ChangeCity(string newCityId)
{
    return RedirectToAction(this.MyDefaultAction);
}

Main question: this schema not so good work. In IE8 sometimes I cant change current city use links like next:

http://www.mysite.com/home/changecity/?newCityId=3

And what you think about this schema at all? May be you use other methods for create functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2024-10-01 00:47:48

有趣的想法;使用 MVC 2 中新的 Html.Action() 功能可能会更好,在您看来,您可以调用 <%= Html.Action("ChangeCity", "Common") %>;在通用控制器中显示更改城市操作,并且有执行此操作的逻辑,而不是为每个操作都执行此操作。

HTH。

Interesting idea; it might be better served by using the new Html.Action() feature in MVC 2, where you can, in your view, call <%= Html.Action("ChangeCity", "Common") %> to display the change city action in a common controller, and in there is the logic to do this, rather than doing it for every action.

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文