6

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

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

EDIT I don't care about models. In my question baseviewA and baseviewB are totally different classes.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174

2 Answers2

9

You can change the base class in Razor with the @inherits keyword, your base classes just need to derive 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
{
}
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Is it possibel to use `<%` syntax in razor instead of `@`? – Zain Shaikh Jan 13 '13 at 13:54
  • @ZainShaikh no the `<%` is not supported in razor only in aspx pages. What is your problem with the usage the `@` symbol? – nemesv Jan 13 '13 at 14:09
  • May be I shall post a separate question for this, but still lets continue. I have a template file which I use with client side javascript (`underscore`) library to build user-interface. I want to render same template file from server side using `razor` engine or some other better than razor. – Zain Shaikh Jan 13 '13 at 17:28
2

Inherits specifies the model type going to be used in the view. Same this can be done in Razor.

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

is equivalent to following in Razor

@model Test.Models.MyModel

this is same in both views and partial views,So

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

is equivalent to

@model Test.Models.MyModelB
Manas
  • 2,534
  • 20
  • 21
  • There is no model business. I just want to change the base class of the view. – Cheng Chen May 08 '12 at 04:43
  • 1
    OK, then the only way would be to keep your views in different folders and have a web.config in each folder you want to override. – Manas May 08 '12 at 04:52
  • Are you sure we can have a few web.config files in each view folder that will `override` the config in the base `view/` folder? – Cheng Chen May 08 '12 at 04:54