【问题标题】:MVC .NET CSS not picked up by the View视图未拾取 MVC .NET CSS
【发布时间】:2010-10-01 01:59:27
【问题描述】:

我正在开发一个 MVC2 站点,但在让我的视图上的对象继承 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>

【问题讨论】:

    标签: c# model-view-controller asp.net-mvc-2


    【解决方案1】:

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

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

    示例:

    CSS:
    .optionlist select
    {
         width:100px;
         height:100px;
    }
    
    <div class="optionlist">
    ... Lisbox
    </div>
    

    【讨论】:

      【解决方案2】:

      当您以这种方式链接您的 css 文件时,并且如果您正在浏览带有类似 http://yoursite.com/MyPage/Content/Article 这样的 url 的页面,当然不会找到 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 链接中添加一个基本网址

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

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

      【讨论】:

        猜你喜欢
        • 2015-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 2018-12-12
        • 1970-01-01
        相关资源
        最近更新 更多