0

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

.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 使用的类别项目类中。

4

3 回答 3

3

如果您的视图中有样式表定义,而不是在 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();
}
于 2011-10-22T21:32:41.047 回答
0

我将只使用多个 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...在这种情况下,我将只使用style值为background-image:url(@item.BackgroundImage).

于 2011-10-24T14:31:31.743 回答
0

使用 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>
于 2017-07-27T15:50:55.160 回答