You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
public abstract class ViewPageBase<TModel> : RazorPage<TModel> {
public String Title { get; set; }
}
On _ViewImports:
@inherits ViewPageBase<TModel>
On _Layout.cshtml:
<title>@Title</title>
On Home/Index:
@{
Title = "Page title"
Layout = "_Layout";
}
1 - I am able to run it but the page title is always empty.
2 - If I have "@model HomeIndexModel" in Home/Index view then Title = "PageTitle" stops being recognized.
The ideal would have @inherits ViewPageBase on _Layout and on ViewPage instead of using ViewImports. This avoids, for example, a component view to have a Title or other properties.
Other approaches are:
1 - Use ViewBag or ViewData as in MVC default template.
I would like to use strong typed for this common view properties.
This is one of the reasons why I have been using a ViewPageBase in MVC5.
The other reasons are solved using injection in MVC6.
2 - Use injection ...
Not sure if this is an option to define common view data.
Anyway, is there a problem with @inherits ViewPageBase?
I am using Beta 7.
Thank You,
Miguel
The text was updated successfully, but these errors were encountered:
@model and @inherits is problematic and I filed #3144 to track it.
The only reliable way of flowing data from Views to their Layouts is using ViewBag \ ViewData - it ensures partials, view component's don't affect what gets passed to the Layout.
That said, if you can ensure the top level view is the only thing that populates the data that needs to go to the layout, you could flow a request scoped data container around.
public class LayoutData
{
public string Title { get; set; }
// Startup
services.AddScoped<LayoutData>();
// _ViewImports
@inject LayoutData LayoutData
// Index.cshtml
@{ LayoutData.Title = "Home Page"; }
I created a ViewPageBase as follows:
On _ViewImports:
On _Layout.cshtml:
On Home/Index:
1 - I am able to run it but the page title is always empty.
2 - If I have "@model HomeIndexModel" in Home/Index view then Title = "PageTitle" stops being recognized.
The ideal would have @inherits ViewPageBase on _Layout and on ViewPage instead of using ViewImports. This avoids, for example, a component view to have a Title or other properties.
Other approaches are:
1 - Use ViewBag or ViewData as in MVC default template.
I would like to use strong typed for this common view properties.
This is one of the reasons why I have been using a ViewPageBase in MVC5.
The other reasons are solved using injection in MVC6.
2 - Use injection ...
Not sure if this is an option to define common view data.
Anyway, is there a problem with @inherits ViewPageBase?
I am using Beta 7.
Thank You,
Miguel
The text was updated successfully, but these errors were encountered: