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

发布于 2024-12-11 12:46:58 字数 1639 浏览 0 评论 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%;
}

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

I have got a category - model which i am using it for my eCommerce system , I have a fixed background image for each category added , What i want to achieve is to programatically add different background image for each category added. Below is the code , currently i am adding images through 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%;
}

Any suggestions or alternatives will be highly appreciated , i just need to add different background images for each category items , At present the background image is fixed in the category-item class used by datalist.

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

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

发布评论

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

评论(3

月寒剑心 2024-12-18 12:46:58

如果您的视图中有样式表定义,而不是在 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();
}

You can do this if you have the stylesheet definition in your view, not in an css file. Css files are, basicly, static files like html. If you want to get some dynamic things to that you have to do it in server side code. Maybe confusing what i say.. but check my sample and you understand what i mean.... i hope ;)

// Your View
<style>
    body 
    {
        background-image: url('@ViewBag.ImagePath');
    }
</style>

// your action method
public ActionResult ActionName()
{
   ViewBag.ImagePath = "<path to your image">
   return View();
}
梦一生花开无言 2024-12-18 12:46:58

我只会使用多个 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 [email protected]"> @*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>

看看我如何添加 [email protected]< /code> 到同一个类声明?您还可以使用更语义化的内容,例如类别名称(如果有的话)等。然后您可以在 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 属性。

I would just use multiple CSS classes, one for the general background image styles and then an individual one for each of the categories that just has the specific background-image style set with the correct image reference.

something like this:

@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 [email protected]"> @*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>

See how I added [email protected] to that same class declaration? You can also use something more semantic like the category name if you've got one, etc. Then you can do this in the 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%;
}

There are some other alternatives as well, specifically if you don't know the image url until the loop executes... in which case I would just use a style attribute with a value of background-image:url(@item.BackgroundImage).

夏了南城 2024-12-18 12:46:58

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

<div class="parallax [email protected]('/', '-')" >
  <section class="page-title">
    <div class="container">
      <h2>@Html.Raw(ViewBag.Title)</h2>
    </div>
  </section>
</div>

Using samandmoore's answer, you can also do this entirely in the View, with the source of the image based on the requested URL (using @Request.RawUrl):

<div class="parallax [email protected]('/', '-')" >
  <section class="page-title">
    <div class="container">
      <h2>@Html.Raw(ViewBag.Title)</h2>
    </div>
  </section>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文