Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Is there a problem with @inherits ViewPageBase<TModel> in Beta 7? #3143

Closed
mdmoura opened this issue Sep 16, 2015 · 3 comments
Closed

Is there a problem with @inherits ViewPageBase<TModel> in Beta 7? #3143

mdmoura opened this issue Sep 16, 2015 · 3 comments
Labels

Comments

@mdmoura
Copy link

mdmoura commented Sep 16, 2015

I created a ViewPageBase as follows:

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

@pranavkm
Copy link
Contributor

@mdmoura, here's what your generated code looks like:

public class Index : ViewPageBase<dynamic>
{
   public string Title { get; set; }

   public void Execute()
   {
      Title = "Page title";
      Layout = "_Layout";
   }
}

public class Layout : ViewPageBase<dynamic>
{
   public string Title { get; set; }

   public void Execute()
   {
      Output.Write("<title>" + Title + "</title>");
   }
}

There's nothing in code that knows attempts to flow the Title property from the Index instance to the Layout instance.

Let me get back to you on the (2) @model bit.

@mdmoura
Copy link
Author

mdmoura commented Sep 16, 2015

There's nothing in code that knows attempts to flow the Title property from the Index instance to the Layout instance.

Yes, that was what I had in mind but was not sure about it ...
Any way of flowing the title from Index to _Layout without using ViewBag / ViewData?

@pranavkm
Copy link
Contributor

@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"; }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants