Asp.net mvc 使用Razor为添加到模型中的每个类别添加不同的背景图像

Asp.net mvc 使用Razor为添加到模型中的每个类别添加不同的背景图像,asp.net-mvc,asp.net-mvc-3,razor,nopcommerce,Asp.net Mvc,Asp.net Mvc 3,Razor,Nopcommerce,我有一个类别模型,我使用它为我的电子商务系统,我有一个固定的背景图像为每个类别添加,我想实现的是,以编程方式为每个类别添加不同的背景图像添加。下面是代码,目前我正在通过css添加图像 @using Nop.Web.Models.Catalog; @if (Model.Count > 0) { <div class="home-page-category-grid"> @(Html.DataList<CategoryModel>(Model, 5,

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

任何建议或替代方案都将受到高度赞赏,我只需要为每个类别项目添加不同的背景图像,目前背景图像固定在datalist使用的类别项目类中。

如果您在视图中有样式表定义,而不是css文件中,则可以执行此操作。Css文件基本上是静态文件,比如html。如果你想得到一些动态的东西,你必须在服务器端代码中完成。也许会把我说的话弄糊涂了。。但看看我的样品,你就明白我的意思了。。。。我希望;)

//您的视图
身体
{
背景图像:url('@ViewBag.ImagePath');
}
//你的行动方法
公共ActionResult ActionName()
{

ViewBag.ImagePath=“如果您的视图中有样式表定义,而不是css文件,则可以执行此操作。css文件基本上是html之类的静态文件。如果您想获得一些动态内容,则必须在服务器端代码中执行。可能会混淆我所说的内容。但是检查我的示例,您就会明白我的意思……我希望;)

//您的视图
身体
{
背景图像:url('@ViewBag.ImagePath');
}
//你的行动方法
公共ActionResult ActionName()
{

ViewBag.ImagePath=“我只会使用多个CSS类,一个用于一般背景图像样式,然后一个单独的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>

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

我只需要使用多个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>

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

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


@Html.Raw(ViewBag.Title)

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


@Html.Raw(ViewBag.Title)
@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>
.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%;
}
<div class="parallax bg-image-@Request.RawUrl.Replace('/', '-')" >
  <section class="page-title">
    <div class="container">
      <h2>@Html.Raw(ViewBag.Title)</h2>
    </div>
  </section>
</div>