Change the razor view base class in the view code

Please note that this is not a duplicate question. I know that we can specify the type of the base view in the razor section views / web.config. But I want my view1, view2 to inherit from baseviewA, and view3, view4 to inherit from baseviewB. In a razor, how can I do it like in an aspx engine:

<%@ Page Language="C#" Inherits="Test.Myproject.Web.Mvc.ViewBase" %> <%@ Control Language="C#" Inherits="Test.Myproject.Web.Mvc.PartialViewBase" %> 

EDIT I don't like models. In my question, baseviewA and baseviewB are completely different classes.

+6
source share
2 answers

You can change the base class in Razor with the @inherits , your base classes just have to get from System.Web.Mvc.WebViewPage .

So your sample:

<%@ Page Language="C#" Inherits="Test.Myproject.Web.Mvc.ViewBase" %>

Will be

@inherits Test.Myproject.Web.Mvc.ViewBase

Where

 public class Test.Myproject.Web.Mvc.ViewBase : System.Web.Mvc.WebViewPage { } 
+9
source

Inherits indicates the type of model to be used in the view. The same thing can be done at Razor.

 <%@ Page Language="C#" Inherits="Test.Myproject.Web.Mvc.ViewBase<Test.Models.MyModel>" % 

equivalent to the following in razor

 @model Test.Models.MyModel 

it's the same in both views and partial views, So

 <%@ Control Language="C#" Inherits="Test.Myproject.Web.Mvc.PartialViewBase<Test.Models.MyModelB>" %> 

equivalently

 @model Test.Models.MyModelB 
+2
source

Source: https://habr.com/ru/post/915153/


All Articles