在ASP.NET MVC剃刀自定义控件
我如何使用自定义控件与ASPNET.MVC剃刀?
How can I use custom controls with ASPNET.MVC Razor?
我想用一个的Razor视图的自定义控件。例如:
I want to use a custom control on a Razor view. for instance:
<mycontrols:something>@Model.MyVar</mycontrols:something>
或
<mycontrols:something myattribute="@Model.MyVar" />
请注意,我的目标是使用来自MvcControl来源,只为小事宋衍涛UI的东西只有很少的控制。
Please note that my goal is to use only few controls derived from MvcControl, only for trivial repetive ui stuffs.
我试图找出类似@Register在视图的顶部写一个语法,但没有成功。
I tried to find out a syntax similar to @Register to write on the top of the view, but without any success.
然后我就去到web.config,加入
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.
有人可以帮助?
...可能是它是一个有点老式的,但有时也定制控件可以让你的code清洁用的!
...Might be it is a little bit old fashion, but sometimes also custom control could be useful to make your code cleaner!
剃刀语法不支持控制的概念都没有。如果你想使用的控制,你将不得不使用ASPX(的WebForms)语法。
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.
不过,recomended MVC模式是使用HTML辅助功能或部分景色。在剃刀你也可以使用快速辅助功能的 @helper
语法。
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.