【问题标题】:Adding Different Background Images for each category added into a model using Razor为使用 Razor 添加到模型中的每个类别添加不同的背景图像
【发布时间】:2011-12-13 08:17:29
【问题描述】:

我有一个类别模型,我将它用于我的电子商务系统,我为添加的每个类别都有一个固定的背景图像,我想要实现的是以编程方式为添加的每个类别添加不同的背景图像。下面是代码,目前我正在通过css添加图像。

@using Nop.Web.Models.Catalog;
@if (Model.Count > 0)
{
<div class="home-page-category-grid">
    @(Html.DataList<CategoryModel>(Model, 5,
            @<div class="item-box">
                <div class="category-item"> @*Thats where i am adding background-images in the class category-item*@
                    <h2 class="title">
                        <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                            @item.Name</a>
                    </h2>
                    <div class="picture">
                        <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                            <img style="border-width: 0px;" alt="@item.PictureModel.AlternateText" src="@item.PictureModel.ImageUrl"
                                title="@item.PictureModel.Title" /></a>
                    </div>
                </div>
            </div>
        ))
</div>
<div class="home-page-category-grid-separator"></div>

}

Css for Category -Item

.home-page-category-grid .category-item
{
text-align: center;
margin: 10px 0px 35px 4px; /*width: 150px;*/
width: 166px; 
height: 185px; 
background: url('images/picture-bg.png') no-repeat 0 100%;
}

任何建议或替代方案将不胜感激,我只需要为每个类别项目添加不同的背景图片,目前背景图片固定在 datalist 使用的类别项目类中。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 razor nopcommerce


    【解决方案1】:

    如果您的视图中有样式表定义,而不是在 css 文件中,则可以执行此操作。 Css 文件基本上是静态文件,如 html。如果你想获得一些动态的东西,你必须在服务器端代码中完成。可能会混淆我所说的话..但检查我的样本,你就会明白我的意思..我希望;)

    // Your View
    <style>
        body 
        {
            background-image: url('@ViewBag.ImagePath');
        }
    </style>
    
    // your action method
    public ActionResult ActionName()
    {
       ViewBag.ImagePath = "<path to your image">
       return View();
    }
    

    【讨论】:

      【解决方案2】:

      我将只使用多个 CSS 类,一个用于一般背景图像样式,然后为每个类别使用一个单独的类,这些类别具有特定的背景图像样式和正确的图像参考。

      类似这样的:

      @using Nop.Web.Models.Catalog;
      @if (Model.Count > 0)
      {
      <div class="home-page-category-grid">
          @(Html.DataList<CategoryModel>(Model, 5,
                  @<div class="item-box">
                      <div class="category-item category-@item.Id"> @*Thats where i am adding background-images in the class category-item*@
                          <h2 class="title">
                              <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                                  @item.Name</a>
                          </h2>
                          <div class="picture">
                              <a href="@Url.RouteUrl("Category", new { categoryId = item.Id, SeName = item.SeName })" title="@item.PictureModel.Title">
                                  <img style="border-width: 0px;" alt="@item.PictureModel.AlternateText" src="@item.PictureModel.ImageUrl"
                                      title="@item.PictureModel.Title" /></a>
                          </div>
                      </div>
                  </div>
              ))
      </div>
      <div class="home-page-category-grid-separator"></div>
      

      看看我是如何将category-@item.Id 添加到同一个类声明中的?如果您有类别名称,您还可以使用更语义化的名称等。然后您可以在 CSS 中执行此操作:

      .home-page-category-grid .category-item
      {
          text-align: center;
          margin: 10px 0px 35px 4px; /*width: 150px;*/
          width: 166px; 
          height: 185px;
      }
      
      .home-page-category-grid .category-item .category-1
      {
          background: url('images/picture-bg-1.png') no-repeat 0 100%;
      }
      
      .home-page-category-grid .category-item .category-2
      {
          background: url('images/picture-bg-2.png') no-repeat 0 100%;
      }
      

      还有一些其他选择,特别是如果您在循环执行之前不知道图像 url...在这种情况下,我将只使用值为 background-image:url(@item.BackgroundImage)style 属性。

      【讨论】:

        【解决方案3】:

        使用 samandmoore 的回答,您也可以完全在视图中执行此操作,图像的来源基于请求的 URL(使用 @Request.RawUrl):

        <div class="parallax bg-image-@Request.RawUrl.Replace('/', '-')" >
          <section class="page-title">
            <div class="container">
              <h2>@Html.Raw(ViewBag.Title)</h2>
            </div>
          </section>
        </div>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-10-30
          • 1970-01-01
          • 2018-06-02
          • 2013-11-17
          • 2021-09-05
          • 2012-02-08
          • 1970-01-01
          相关资源
          最近更新 更多