C# 如何在Orchard 1.7.1的内容菜单项中添加图像而不是文本
我已阅读并尝试重现此问题/答案中的步骤C# 如何在Orchard 1.7.1的内容菜单项中添加图像而不是文本,c#,razor,orchardcms-1.7,C#,Razor,Orchardcms 1.7,我已阅读并尝试重现此问题/答案中的步骤 已使用“图像”内容选择器字段创建MenuImagePart 已将此部分添加到“内容菜单项” 更改了MenuItemLink-ContentMenuItem.cshtml中的代码 但下一个错误是: @使用Orchard.Utility.Extensions; @使用系统。动态 @{ /*获取菜单内容项 ***************************************************************/ var菜单=Model.C
@使用Orchard.Utility.Extensions;
@使用系统。动态
@{
/*获取菜单内容项
***************************************************************/
var菜单=Model.Content.ContentItem;
/*基于菜单项创建唯一的CSS类名
***************************************************************/
//!!!由于某些原因,以下代码抛出:“字符串”不包含“HtmlClassify”的定义
//字符串测试=menu.ContentType.HtmlClassify();
string cssPrefix=Orchard.Utility.Extensions.StringExtensions.HtmlClassify(menu.ContentType);
var uniquecscsclassname=cssPrefix+'-'+Model.Menu.MenuName;
/*将普通样式和悬停样式添加到html(如果有)
***************************************************************/
如果(menu.MenuImagePart!=null)
{
如果(!string.IsNullOrWhiteSpace(menu.MenuImagePart.Image.Url))
{
使用(Script.Head()){
.@uniquecsclassname{
背景图像:url('@Href(menu.MenuImagePart.image.url)');
宽度:@{@menu.MenuImagePart.Image.width}px;
高度:@{@menu.MenuImagePart.Image.height}px;
显示:块;
}
}
}
如果(!string.IsNullOrWhiteSpace(menu.MenuImagePart.HoverImage.Url))
{
使用(Script.Head()){
.@uniquecsclassname:hover{
背景图像:url('@Href(menu.MenuImagePart.HoverImage.url)');
宽度:@{@menu.MenuImagePart.HoverImage.width}px;
高度:@{@menu.MenuImagePart.HoverImage.height}px;
}
}
}
}
}
我做错了什么?我应该如何创建此图像以添加到新创建的“内容菜单项”?如果您有一个contentpicker字段,您需要一个mediapickerfield(如果您是Orchard 1.6) 谢谢你的回答,但是当你想使用MediaGalleryField时,我使用的是1.7.1(我相信)。ContentPickerField错了
@using Orchard.Utility.Extensions;
@using System.Dynamic
@{
/* Getting the menu content item
***************************************************************/
var menu = Model.Content.ContentItem;
/* Creating a unique CSS class name based on the menu item
***************************************************************/
// !!! for some reason the following code throws: 'string' does not contain a definition for 'HtmlClassify'
//string test = menu.ContentType.HtmlClassify();
string cssPrefix = Orchard.Utility.Extensions.StringExtensions.HtmlClassify(menu.ContentType);
var uniqueCSSClassName = cssPrefix + '-' + Model.Menu.MenuName;
/* Adds the normal and hovered styles to the html if any
***************************************************************/
if (menu.MenuImagePart != null)
{
if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.Image.Url))
{
using(Script.Head()){
<style>
.@uniqueCSSClassName {
background-image: url('@Href(menu.MenuImagePart.Image.Url)');
width: @{@menu.MenuImagePart.Image.Width}px;
height: @{@menu.MenuImagePart.Image.Height}px;
display: block;
}
</style>
}
}
if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.HoverImage.Url))
{
using(Script.Head()){
<style>
.@uniqueCSSClassName:hover {
background-image: url('@Href(menu.MenuImagePart.HoverImage.Url)');
width: @{@menu.MenuImagePart.HoverImage.Width}px;
height: @{@menu.MenuImagePart.HoverImage.Height}px;
}
</style>
}
}
}
}
<a class="@uniqueCSSClassName" href="@Model.Href">@Model.Text</a>