使用 ASP.NET MVC Razor 自定义控件 [英] Custom controls with ASP.NET MVC Razor

查看:23
本文介绍了使用 ASP.NET MVC Razor 自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ASPNET.MVC Razor 中使用自定义控件?

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.

有人可以帮忙吗?

...可能有点过时了,但有时自定义控件也可能有助于使您的代码更简洁!

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

推荐答案

Razor 语法根本不支持控件的概念.如果要使用控件,则必须使用 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.

然而,推荐的 MVC 模式是使用 html helper 函数或部分视图.在 Razor 中,您还可以使用 @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.

这篇关于使用 ASP.NET MVC Razor 自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆