Umbraco 7 - if 字段有问题

Umbraco 7 - having trouble with if field

我对此很陌生,正在使用 Umbraco v7 中已经存在的网站。

当 属性 别名 freeProgram 为 true 时,我正在尝试显示 Free 一词。

FreeProgram 已经在文档类型程序上设置了一个复选框

它被用来创建过滤器并且在一些地方被引用

此内容是根据部分模板创建的 - 这是代码(如果 freeProgram 为真,则包括许多创建内容的尝试)

@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>
}

这是包含 VMCard 的模型 class

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, bool small = false, 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 属性 别名最初是在 属性 模型中设置的。这是 属性 模型 header 和相关代码。

            //------------------------------------------------------------------------------
// <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 创建的模型与如何在其他命名空间中使用它们之间的关系(我什至不确定什么是命名空间,但我猜它以某种方式将它们分开)

所以我想做的是在设置了 属性 别名的项目中添加免费一词。我哪里错了?非常感谢。我的截止日期很紧,因为有人告诉我这是一个 wordpress 网站,现在他们找不到其他人来做,网站将在两天内上线……呃……

如果该部分模板实际呈现并且模型 VM_Cards 已正确填充,那么您应该能够做到

@if(Model.freeProgram) {
   <p>Really free</p>
}

当您尝试显示 属性 的值时是否遇到任何错误?

此外,class VM_Cards 有几个不同的构造函数,freeProgram 没有在所有构造函数中初始化。

你能展示一下传递给视图的模型是如何构建的吗?您的代码中某处可能有一个控制器(.cs 文件)构造传递给视图的模型?在代码库中搜索部分视图的名称(没有 .cshtml),您应该能够找到将 VM_Cards 传递给视图的代码。