I have a Master view in my MVC solution which requires data (Companies) from a separate database to the Umbraco CMS database.
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>
I then have Template views in Umbraco which use this master view as a layout.
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")
When a controller which should generate this view (a Document named Home which uses ChildNodeSelectionPage.cshtml as its Template) is first hit, the model is null and I can't create it! What do I need to do to get the model to not be null?
MVC Home Controller:
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
);
}
}
Master Model:
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;
}
}
Please note I'm new to Umbraco and still trying to figure out the best way to integrate it with an existing MVC website. If the approach I have taken here is wrong, feel welcome to suggest another to avert this problem I am having with the controller not receiving a model.