【问题标题】:Why is my MVC controller getting a null Umbraco RenderModel?为什么我的 MVC 控制器得到一个空的 Umbraco RenderModel?
【发布时间】:2018-11-21 17:14:46
【问题描述】:

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

Shared\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-controller umbraco umbraco7


    【解决方案1】:

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

    我的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“主页”文档。

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多