MVC .NET CSS 未被视图拾取

发布于 2024-09-25 19:25:35 字数 594 浏览 6 评论 0原文

我正在 MVC2 站点上工作,并且在让我的视图上的对象继承 css 类时遇到问题。

这是我的辅助对象和 CSS,保存在 Site.css 中,该 CSS 链接在母版页中。

如果我将 CSS 放在母版页的标签中,这也可以正常工作。

<%= Html.ListBox("ExpenseItems", (System.Web.Mvc.SelectList)ViewData["ExpenseDefinitions"], new { @class = "optionlist" })%>

.optionlist
{
    width:100px;
    height:100px;
}

Browser HTML:
..
<link href="../Content/Site.css" rel="stylesheet" type="text/css" />
..
<select class="optionlist" id="ExpenseItems" multiple="multiple" name="ExpenseItems">
<option value="1">Test</option>
</select>

I am working on a MVC2 site and am having issues getting my objects on my views to inherit the css classes.

Here is my helper object and CSS that is saved in the Site.css which is linked in the master page.

This also works fine if I put the CSS in a tag on the masterpage.

<%= Html.ListBox("ExpenseItems", (System.Web.Mvc.SelectList)ViewData["ExpenseDefinitions"], new { @class = "optionlist" })%>

.optionlist
{
    width:100px;
    height:100px;
}

Browser HTML:
..
<link href="../Content/Site.css" rel="stylesheet" type="text/css" />
..
<select class="optionlist" id="ExpenseItems" multiple="multiple" name="ExpenseItems">
<option value="1">Test</option>
</select>

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

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

发布评论

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

评论(2

白日梦 2024-10-02 19:25:35

想通了...无法将样式应用到列表。

出于某种原因,您需要将其应用于 div,然后应用于 CSS 中的控件。

例子:

CSS:
.optionlist select
{
     width:100px;
     height:100px;
}

<div class="optionlist">
... Lisbox
</div>

Figured it out... Can't apply the style to the list.

Some reason, you need to apply it to a div then apply to the control in CSS.

example:

CSS:
.optionlist select
{
     width:100px;
     height:100px;
}

<div class="optionlist">
... Lisbox
</div>
隱形的亼 2024-10-02 19:25:35

当您以这种方式链接 css 文件时,并且如果您正在浏览具有这样的 url 的页面 http://yoursite.com/MyPage/Content/Article 当然,css 文件不会被发现,因为它是这样走的。

css file mapped in `../Content/Sites.css`
Page is `/MyPage/Content/Article` 
css real content is placed in `/Content`
when the parser looks for the css it looks in `/MyPage/Content/Site.css`
which is not where it where it is.

我的建议是在你的css链接中添加一个基本url

<%
    string baseUrl = "http://" + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port.ToString() : "");
gt;
<link href=<%=baseUrl%>/Content/Site.css rel="stylesheet" type="text/css" />

不要将"放在链接标签的href中

when you link your css file that way, and if you are browing in in a page with a url like this http://yoursite.com/MyPage/Content/Article of course the css file will not be found since it goes this way.

css file mapped in `../Content/Sites.css`
Page is `/MyPage/Content/Article` 
css real content is placed in `/Content`
when the parser looks for the css it looks in `/MyPage/Content/Site.css`
which is not where it where it is.

My suggestion is add a base url to your css link

<%
    string baseUrl = "http://" + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port.ToString() : "");
gt;
<link href=<%=baseUrl%>/Content/Site.css rel="stylesheet" type="text/css" />

Don't put " in href of the link tag

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