使用 Razor 为添加到模型中的每个类别添加不同的背景图像
我有一个类别模型,我将其用于我的电子商务系统,我为添加的每个类别都有一个固定的背景图像,我想要实现的是以编程方式为添加的每个类别添加不同的背景图像。下面是代码,目前我正在通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的视图中有样式表定义,而不是在 css 文件中,则可以执行此操作。 CSS 文件基本上是像 html 一样的静态文件。如果你想获得一些动态的东西,你必须在服务器端代码中完成。也许我说的话令人困惑..但是检查我的样本,你就会明白我的意思....我希望;)
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 ;)
我只会使用多个 CSS 类,一个用于一般背景图像样式,然后一个用于每个类别的单独一个,该类别仅具有带有正确图像引用的特定背景图像样式集。
像这样的内容:
看看我如何添加
[email protected]< /code> 到同一个类声明?您还可以使用更语义化的内容,例如类别名称(如果有的话)等。然后您可以在 CSS 中执行此操作:
还有其他一些替代方案,特别是如果您在循环之前不知道图像 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:
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: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 ofbackground-image:url(@item.BackgroundImage)
.使用 samandmoore 的答案,您还可以完全在视图中执行此操作,并根据请求的 URL(使用 @Request.RawUrl)使用图像源:
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):