首页 > 解决方案 > 为什么我的 MVC 控制器得到一个空的 Umbraco RenderModel?

问题描述

我的 MVC 解决方案中有一个主视图,它需要从单独的数据库到 Umbraco CMS 数据库的数据(公司)。

共享\Master.cshtml

@inherits Umbraco.Web.Mvc.UmbracoViewPage<MyCode.Web.Portal.Models.Master>
<!DOCTYPE html>
<html>
    <head>
        <title>@Umbraco.Field("title")</title>
    </head>
    <body>
        <div>
            <span>
                <h1>@Umbraco.GetDictionaryValue("Application Name")</h1>
            </span>
            <span>
                    <h1>@Umbraco.GetDictionaryValue("Company"):</h1>
                <!--This is the data from a separate database.-->
                @Html.DropDownListFor(model => model.SelectedCompany, new SelectList(Model.Companies))
            </span>
        </div>
        @Html.Partial("Navigation")
        <div class="container">
            @Html.Partial("Breadcrumb")
            <div class="body-content">
                <!--This is where I expect the Umbraco view to be nested.-->
                @RenderBody()
            </div>
        </div>
    </body>
</html>

然后我在 Umbraco 中有模板视图,它使用这个主视图作为布局。

ChildNodeSelectionPage.cshtml

@{
    Layout = "Shared/Master.cshtml";
}
<img src="@Model.Content.GetProperty("icon")"/>
<h2>@Model.Content.GetProperty("title")</h2>
@Html.Raw(Model.Content.GetProperty("description"))
@Html.Partial("Child Node Grid")

当一个应该生成这个视图的控制器(一个名为 Home 的 Document 使用 ChildNodeSelectionPage.cshtml 作为它的模板)被第一次命中时,模型是空的,我不能创建它!我需要做什么才能使模型不为空?

MVC 家庭控制器:

public class HomeController : RenderMvcController
{
    private ActionResult Index(IPublishedContent content, CultureInfo currentCulture)
        => CurrentTemplate
        (
            new Master
            (
                content,
                currentCulture,
                new Company(0, "Automobilli Lamborghini Spa"),
                new[]
                {
                    new Company(0, "Automobilli Lamborghini Spa"),
                    new Company(1, "Ital Design")
                }
            )
        );

    public override ActionResult Index(RenderModel model)
        //The model is null, the PublishedContentRequest is null and Umbraco.TypedContentSingleAtXPath fails!  I can't even hack myself a fresh model!
        => Index
        (
            model?.Content ?? UmbracoContext.Current.PublishedContentRequest?.PublishedContent ?? Umbraco.TypedContentSingleAtXPath("Home"),
            model?.CurrentCulture ?? UmbracoContext.Current.PublishedContentRequest?.Culture ?? CultureInfo.CurrentUICulture
        );
    }
}

主模型:

public class Master : RenderModel
{
    public Company SelectedCompany { get; }
    public IEnumerable<Company> Companies { get; }

    public Master
    (
        IPublishedContent content,
        CultureInfo culture,
        Company selectedCompany,
        IEnumerable<Company> companies
    )
        : base
        (
            content,
            culture
        )
    {
        SelectedCompany = selectedCompany;
        Companies = companies;
    }
}

请注意,我是 Umbraco 的新手,仍在尝试找出将其与现有 MVC 网站集成的最佳方法。如果我在这里采取的方法是错误的,欢迎提出另一种方法来避免我在控制器没有接收模型时遇到的这个问题。

标签: c#model-view-controllerumbracoumbraco7

解决方案


问题出在我的RouteConfig.cs文件中,因为我正在调用MapRoute它,这反过来又覆盖了 Umbraco 自己的路由,因此不允许它为它的模板传递模型。我删除了这个调用并重命名了 myHomeController以匹配我用于主页的 Umbraco 模板的名称,然后 Umbraco 能够Index在传递RenderModel.

我的RouteConfig班级现在看起来像这样:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //This code was the source of the problem.
        //routes.MapRoute
        //(
        //  name: "Default",
        //  url: "{controller}/{action}/{id}",
        //  defaults: new
        //  {
        //      controller = "Home",
        //      action = "Index",
        //      id = UrlParameter.Optional
        //  }
        //);
    }
}

确保此控制器的名称与 Umbraco 文档使用的 Umbraco 模板的文件名匹配,但后缀为“控制器”。

//Previously named HomeController.
public class ChildNodeSelectionPageController : RenderMvcController

现在匹配模板文件名称 +“控制器”(参见“子节点选择页面”可编辑文本框下)。

Umbraco 模板名称

这是使用该模板的 Umbraco“主页”文档。

使用模板的 Umbraco 文档


推荐阅读