如何解决Umbraco 7-if字段
我对此很陌生,正在使用Umbraco v7中已经存在的网站。
我试图在属性别名freeProgram为true时显示Free一词。
FreeProgram已经在文档类型程序上设置了一个复选框
它被用来创建过滤器,并且在一些地方被引用
此内容是通过部分模板创建的-这是代码(如果freeProgram为true,则包括创建内容的许多尝试中的一部分)
@model PerformanceSpace.Models.VM_Cards
@{ var imgurl = String.Format("{0}?width={1}&height={2}&mode=crop",Model.imgUrl,Model.doubleCrop ? 700 : 400,Model.doubleCrop ? 400 : Model.smallCard ? 400 : 496); }
<div class="card-image">
@if (!String.IsNullOrEmpty(Model.imgUrl))
{
<img src="@imgurl" alt="@Model.imgAlt">}
</div>
@if (Model.smallCard)
{
<div class="py16 px16">
<h3 class="h6 mb0 card--title" style="color:@Model.txtColour !important">
@Model.title
</h3>
</div> }
else
{
<h3 class="h6 mb0 card__heading">
<span class="text--hl card--title" style="background-color:@Model.colour;">
@Model.title
</span>
</h3>}
<div class="card__date mt16" style="color:@Model.txtColour !important">
<small>@Model.subTitle</small>
</div>
</a>
@{
if (!Model.Value<bool>("freeProgram"))
{
<p>The Checkbox is not checked!</p>
}
}
当前,这可防止打开物品。追溯过去(记住我对此一无所知)
我尝试了许多不同的语法,并复制了控制器中用于获取相同内容的if语句
@if (model.freeProgram)
{
<div>free</div>
}
这是包含VMCards类的模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using Umbraco.Web;
using Umbraco.Web.PublishedContentModels;
namespace PerformanceSpace.Models
{
public class VM_RelatedCards
{
public string areaTitle { get; set; }
public bool pastOnly { get; set; }
public string pastURL { get; set; }
public string currentURL { get; set; }
public int displayLimit { get; set; }
public string card1Colour { get; set; }
public string cardTextColour { get; set; }
public string card2Colour { get; set; }
public string card3Colour { get; set; }
public bool altSizes { get; set; }
public bool doubleSizeFirst { get; set; }
public bool noCurrentPast { get; set; }
public bool showline { get; set; }
public List<VM_Cards> cards { get; set; }
public VM_RelatedCards()
{
init();
}
public VM_RelatedCards(IEnumerable<Artist> inputList)
{
init(inputList);
}
public VM_RelatedCards(IEnumerable<Program> inputList)
{
init(inputList);
}
public VM_RelatedCards(IEnumerable<Umbraco.Core.Models.IPublishedContent> inputList)
{
init(inputList);
}
private void init(IEnumerable<Umbraco.Core.Models.IPublishedContent> input = null,bool sizeAlternation = false)
{
pastOnly = true;
displayLimit = 3;
cards = new List<VM_Cards>();
altSizes = sizeAlternation;
doubleSizeFirst = false;
noCurrentPast = false;
showline = true;
if (input != null && input.Count() > 0) {
var T = input.First().GetType();
if (T == typeof(Artist)) {
foreach (Artist item in input) {
cards.Add(new VM_Cards(item)); } }
else if(T == typeof(Program)) {
foreach (Program item in input) {
cards.Add(new VM_Cards(item)); } }
else if(T== typeof(BlogPost)) {
foreach(BlogPost item in input) {
cards.Add(new VM_Cards(item)); } } }
}
private void init(IEnumerable<Artist> inputList)
{
init();
for (int count = 0; count < inputList.Count(); count++) {
var item = inputList.ElementAt(count);
cards.Add(new VM_Cards(item)); }
}
private void init(IEnumerable<Program> inputList)
{
init();
foreach (var item in inputList) {
cards.Add(new VM_Cards(item)); }
}
}
public class VM_Cards
{
public string colour { get; set; }
public string txtColour { get; set; }
public string title { get; set; }
public string subTitle { get; set; }
public string imgUrl { get; set; }
public string imgAlt { get; set; }
public string linkURL { get; set; }
public bool smallCard { get; set; }
public string cellClass { get; set; }
public bool doubleCrop { get; set; }
public bool freeProgram { get; set; }
public bool onlineProgram { get; set; }
public VM_Cards()
{
smallCard = false;
doubleCrop = false;
freeProgram = false;
}
public VM_Cards(Artist input,bool small = false,string textColour = null)
{
this.colour = input.HeadingColours.GetPropertyValue<string>("backgroundColour");
txtColour = textColour ?? gFuncs.textColourFromBG(colour);
title = input.FirstName + " " + input.LastName;
subTitle = input.HeadingText;
imgUrl = input.BiographyImage != null ? input.BiographyImage.Url : input.HeadingHeroImage.Url;
imgAlt = input.BiographyImage == null ? input.HeadingHeroImage.Name : input.BiographyImage.Name;
smallCard = small;
cellClass = "mb32";
linkURL = input.Url;
doubleCrop = false;
}
public VM_Cards(Program input,string textColour = null)
{
string format = (input.ProgramDates != null) ? ((List<Umbraco.Core.Models.IPublishedContent>)input.ProgramDates).ToMultiDate().MaxDate().Date <= DateTime.Now.AddMonths(-8) ? "MMM d,yyyy" : "MMM d" : null;
this.colour = input.HeadingColours.GetPropertyValue<string>("backgroundColour");
title = input.Title;
subTitle = input.ProgramDates != null ? input.ProgramDates.ToList().ToMultiDate().MinDate().ToString(format) + ((input.ProgramDates.Count() > 1) ? " - " + input.ProgramDates.ToList().ToMultiDate().MaxDate().ToString(format) : "") : "";
imgUrl = input.HeadingHeroImage.Url;
linkURL = input.Url;
smallCard = small;
txtColour = textColour ?? gFuncs.textColourFromBG(colour);
cellClass = "mb32";
doubleCrop = false;
freeProgram = "free";
}
public VM_Cards(program input,bool small = false)
{
string format = input.dateFinish.Year < DateTime.Now.Year ? "MMM d,yyyy" : "MMM d";
colour = input.bgColour;
txtColour = gFuncs.textColourFromBG(input.fgColour);
title = input.heading;
if (input.dateStart == new DateTime())
{
subTitle = "TBC";
}
else
{
subTitle = input.dateStart.ToString(format) + (input.dateStart.Date != input.dateFinish.Date ? " - " + input.dateFinish.ToString(format) : null);
}
imgUrl = input.img;
linkURL = input.url;
smallCard = small;
cellClass = "mb32";
doubleCrop = false;
freeProgram = "free";
}
public VM_Cards(BlogPost input,bool dblCrop = false)
{
//string format = "dddd MMMM dd yyyy";
title = input.Title;
//subTitle = input.CreateDate.ToString(format);
imgUrl = input.FeatureImage.Url;
imgAlt = input.FeatureImage.Name;
linkURL = input.Url;
colour = input.CardColour as string;
txtColour = gFuncs.textColourFromBG(colour);
doubleCrop = dblCrop;
}
}
}
因此,据我了解,这似乎不是通过创建文档类型创建的模型-没有称为VM的文档类型
freeProgam属性别名最初是在Property模型中设置的。这是属性模型头和相关代码。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Umbraco.ModelsBuilder v3.0.10.102
//
// Changes to this file will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;
namespace Umbraco.Web.PublishedContentModels
{
.
.
.
///<summary>
/// Free Program: Is this a free program?
///</summary>
[ImplementPropertyType("freeProgram")]
public bool FreeProgram
{
get { return this.GetPropertyValue<bool>("freeProgram"); }
}
我想我不理解用modelsbuilder创建的模型与如何在另一个名称空间中使用它们之间的关系(我什至不确定该名称空间是什么,但我猜想它会以某种方式将它们分开)
所以我想做的是在设置了该属性别名的项目中添加free这个词。我要去哪里错了?非常感谢。有人告诉我这是一个Wordpress网站,我的期限很紧,现在他们找不到其他人可以这样做,而且该网站在两天内上线了……呃……。
解决方法
如果该部分模板实际上已渲染并且正确填充了模型VM_Cards,那么您应该能够做到
@if(Model.freeProgram) {
<p>Really free</p>
}
尝试显示该属性的值时遇到任何错误吗?
此外,类VM_Cards具有一些不同的构造函数,并且freeProgram并未在所有构造函数中初始化。
您能显示传递给视图的模型是如何构建的吗?在代码中的某个位置可能有一个控制器(.cs文件),该控制器构造了传递给视图的模型?在代码库中搜索部分视图的名称(不带.cshtml),您应该能够找到将VM_Cards传递到视图的代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。