首页 > 解决方案 > c#“x”不包含“y”的定义

问题描述

由于我对 C# 和 OOP 的初级理解,我遇到了一些问题。但是我一直无法通过寻找答案来理解我做错了什么。

我的模型 - KeyVisualBlock.cs:

[Display(Order = 90,
GroupName = SystemTabNames.Content)]
[AllowedTypes(typeof(FormContainerBlock))]
public virtual ContentReference ContactForm { get; set; }

我的控制器 - KeyVisualBlockController.cs:

using System.Web.Mvc;

using Example.Site.Models.Blocks;
using Example.Site.Models.Pages;
using Example.Site.Models.ViewModels;

using EPiServer;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using EPiServer.Web.Mvc;

namespace Example.Site.Controllers
{
    public class KeyVisualBlockController : BlockController<KeyVisualBlock>
    {
        public override ActionResult Index(KeyVisualBlock currentBlock)
        {
            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
            var startPage = contentLoader.Get<StartPage>(SiteDefinition.Current.StartPage);

            KeyVisualBlockViewModel model = new KeyVisualBlockViewModel
            {
                SearchLink = startPage.SearchPageLink,
                CurrentKeyVisualBlock = currentBlock
            };

            return PartialView("KeyVisualBlock", model);
        }

    }

    public class KeyContactBlockController : BlockController<ContactBlock>
    {
        public override ActionResult Index(ContactBlock currentBlock)
        {
            ContactBlockViewModel model = new ContactBlockViewModel(currentBlock);

            return PartialView("~/Views/Shared/Blocks/ContactBlock.cshtml", model);
        }
    }
}

我的视图模型:

using Example.Site.Models.Blocks;

using EPiServer;
using EPiServer.Core;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.ServiceLocation;
using System;

namespace Example.Site.Models.ViewModels
{
    public class KeyVisualBlockViewModel
    {
        public PageReference SearchLink { get; set; }
        public KeyVisualBlock CurrentKeyVisualBlock { get; set; }
    }

    public class KeyContactBlockViewModel
    {
        public KeyContactBlockViewModel(ContactBlock KeycurrentBlock)
        {
            IContentLoader contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

            this.KeyCurrentBlock = KeycurrentBlock;

            if (!ContentReference.IsNullOrEmpty(this.KeyCurrentBlock.ContactForm))
            {
                this.ContactFormID = contentLoader.Get<FormContainerBlock>(this.KeyCurrentBlock.ContactForm)?.Form?.FormGuid;
            }
        }

        public ContactBlock KeyCurrentBlock { get; set; }
        public Guid? ContactFormID { get; set; }
    }
}

最后是我的观点:

<header>
    <h2 id="@(string.Format("formWrapperHeader_{0}", Model.ContactFormID))"
        data-header-success-message="@Model.KeyCurrentBlock.SuccessHeadline">
        @Html.PropertyFor(x => x.KeyCurrentBlock.Headline)
    </h2>
</header>

我在视图中得到的错误如下:

“KeyVisualBlockViewModel”不包含“KeyCurrentBlock”的定义,并且找不到接受“KeyVisualBlockViewModel”类型的第一个参数的可访问扩展方法“KeyCurrentBlock”(您是否缺少 using 指令或程序集引用?)

我要做的是添加将表单包含到我们的 KeyVisualBlock 中的功能,以便我们的编辑器可以将表单包含到我们的页眉部分中。

标签: c#asp.net-mvc

解决方案


推荐阅读