使用 MVC 根据会话值启用/禁用 CSS
我对 MVC 应用程序相当陌生,我想要完成的一件事是根据会话值启用或禁用样式表。
我以这种方式在我的 Site.Master 页面中引用了样式表:
<%=Html.Stylesheet("~/styles/main.css", "string")%> <%=Html.Stylesheet("~/styles/additions.css", "string")%>
现在,为了进行测试,我在 Html.Stylesheet 标签周围放置了一条 if 语句:
<% if (Session["cssRule"] = "enableCss") { %>;
<%=Html.Stylesheet("~/styles/main.css", "screen")%> <%=Html.Stylesheet("~/styles/additions.css", "screen")%>
<%}%>
因此,如果“cssRule”会话值为 null,则不会加载 CSS。目前这工作正常,但并不完全是我想要的。现在,当用户登录时,我在控制器中设置会话值,但最终我需要根据用户是否单击启用或禁用按钮来设置会话变量的值。由于过去一年半我主要使用 Web 表单,所以我只想删除一个超链接并为其设置一个事件,但是可惜,这是 MVC 的未来,所以我需要弄清楚如何做类似的事情所以
我真正的问题是如何通过使用 MVC 单击链接来设置“cssRule”值的会话?
I am rather new to MVC applications, and one thing I am trying to accomplish is enabling or disabling stylesheets based on a Session value.
I have stylesheets referenced in my Site.Master page in this manner:
<%=Html.Stylesheet("~/styles/main.css", "string")%>
<%=Html.Stylesheet("~/styles/additions.css", "string")%>
Right now, for testing, I have been putting an if statement around the Html.Stylesheet tags saying:
<% if (Session["cssRule"] = "enableCss") { %>
<%=Html.Stylesheet("~/styles/main.css", "screen")%>
<%=Html.Stylesheet("~/styles/additions.css", "screen")%>
<%} %>
So if the 'cssRule' Session value is null, no CSS loads. Currently this is working fine, but it is not exactly what I am looking for. Right now I set the Session value in the Controller when the user logs in, but ultimately I need to set the value of the Session variable depending on if a user clicks the enable or disable button. Since I have been primarily using webforms for the past year and a half, I just want to drop a hyperlink and set an event for it, but alas, this is the future of MVC so I need to figure out how I can do something like this..
So my real question is how can I set the Session of the "cssRule" value by clicking a link using MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设你想使用标准链接(不是ajax)并且你的主视图是索引
只需在你的控制器中添加一个方法(伪代码)
然后在你的视图中使用
你可以使用很多奇特的不同方法来获得相同的结果;使用 ajax 并重新加载页面,或者不使用,或者重定向到列出要应用的 css 文件的页面等...但这应该可以工作:)
I'll assume you want to use a standard link (not ajax) and that your main view is Index
Just add a method in your controller (pseudocode)
Then in your view, use
You can use lots of fancy different methods to obtain the same result; use ajax and relaod the page, or not, or redirect to a page listing css files to apply, etc... but this one should work :)