我在我的MVC解决方案中有一个Master视图,它需要从单独的数据库到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的文档,使用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网站集成的最佳方式。如果我在这里采取的方法是错误的,欢迎建议另一个人避免这个问题我控制器没有收到模型。
问题出在我的RouteConfig.cs
文件中,因为我调用了MapRoute
,而HomeController
又覆盖了Umbraco自己的路由,因此不允许它传递模板中的模板。我删除了这个调用并重命名了我的Index
以匹配我用于主页的Umbraco模板的名称,然后Umbraco能够在传递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
// }
//);
}
}
课程现在看起来像这样:
//Previously named HomeController.
public class ChildNodeSelectionPageController : RenderMvcController
确保此控制器的名称与Umbraco文档使用的Umbraco模板的文件名匹配,但后缀为“Controller”。
现在匹配模板文件名+“控制器”(参见“子节点选择页面”可编辑文本框下)。
这是使用该模板的Umbraco“Home”文档。
qazxswpoi