【发布时间】: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