ASP.NET MVC:城市列表并将所选城市存储在 cookie 中
我的网站上有城市列表,该列表放置在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有趣的想法;使用 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.