Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom controls with ASP.NET MVC Razor

How can I use custom controls with ASPNET.MVC Razor?

I want to use a custom control on a Razor view. for instance:

<mycontrols:something>@Model.MyVar</mycontrols:something>

or

<mycontrols:something myattribute="@Model.MyVar" />

Please note that my goal is to use only few controls derived from MvcControl, only for trivial repetive ui stuffs.

I tried to find out a syntax similar to @Register to write on the top of the view, but without any success.

Then I went to the web.config, adding

<pages>
   <controls>
      <add tagPrefix="mycontrols" namespace="mynamespace" assembly="myassembly"/>
   </controls>
</pages>

but it looks like custom controls are ignored in rendering.

Someone could help?

...Might be it is a little bit old fashion, but sometimes also custom control could be useful to make your code cleaner!

like image 566
Fabrizio Avatar asked Jan 03 '11 07:01

Fabrizio


1 Answers

The Razor syntax does not support the notion of Controls at all. If you want to use controls you will have to use the ASPX (WebForms) syntax.

However, the recomended MVC pattern is to use html helper functions or partial views. In Razor you can also use the @helper syntax for quick helper functions.

like image 71
marcind Avatar answered Sep 23 '22 12:09

marcind