asp.net-mvc 使用 ASP.NET MVC Razor 自定义控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4582866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:40:48  来源:igfitidea点击:

Custom controls with ASP.NET MVC Razor

asp.net-mvcasp.net-mvc-3razor

提问by Fabrizio

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

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

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

我想在 Razor 视图上使用自定义控件。例如:

<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.

请注意,我的目标是仅使用从 MvcControl 派生的少数控件,仅用于琐碎的重复 ui 内容。

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

我试图找出类似于@Register 的语法来写在视图的顶部,但没有任何成功。

Then I went to the web.config, adding

然后我去web.config,添加

<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!

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

回答by marcind

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.

Razor 语法根本不支持控件的概念。如果要使用控件,则必须使用 ASPX (WebForms) 语法。

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

但是,推荐的 MVC 模式是使用 html 辅助函数或部分视图。在 Razor 中,您还可以使用@helper快速帮助函数的语法。

回答by Darin Dimitrov

In ASP.NET MVC custom server controls should be avoided. Most of them rely on ViewState and PostBack which are notions that no longer exist in MVC. You should prefer using templates, HTML helpers to implement some reusable functionality. Another problem with controls is most of them encapsulate some business logic which fetches data from somewhere and renders it which is an anti-MVC pattern. In MVC it is the controller responsibility to manipulate the model and fetch data and pass a view model to the view which simply should display it.

在 ASP.NET MVC 中应该避免使用自定义服务器控件。它们中的大多数依赖于 ViewState 和 PostBack,这些概念在 MVC 中不再存在。您应该更喜欢使用模板、HTML 助手来实现一些可重用的功能。控件的另一个问题是它们中的大多数封装了一些从某处获取数据并呈现它的业务逻辑,这是一种反 MVC 模式。在 MVC 中,控制器负责操作模型并获取数据并将视图模型传递给应该显示它的视图。

回答by Tom Clarkson

MVC uses partial views rather than custom controls, and they can be used in two ways that cover pretty much everything a custom control can do

MVC 使用部分视图而不是自定义控件,它们可以通过两种方式使用,几乎涵盖了自定义控件可以做的所有事情

  • RenderPartial which renders data already retrieved by the page controller
  • RenderAction which is similar but has its own controller action so can get data independently
  • RenderPartial 呈现页面控制器已经检索到的数据
  • RenderAction 类似但有自己的控制器动作,因此可以独立获取数据

The only scenario I can think of where it would be worth putting a custom control on an mvc view is if you are working on a partially migrated webforms project, and I doubt that would work with anything other than the WebFormsViewEngine.

我能想到的唯一一个值得在 mvc 视图上放置自定义控件的情况是,如果您正在处理部分迁移的 webforms 项目,并且我怀疑这是否适用于 WebFormsViewEngine 以外的任何其他内容。

回答by Patrick Lee Scott

You can do this, though I don't recommend it, though I'm not here to judge. I might add that I don't expect postback and view state to continue working either but you can at least render all of the html.

你可以这样做,虽然我不推荐这样做,但我不是来评判的。我可能会补充说,我不希望回发和视图状态继续工作,但您至少可以呈现所有 html。

The only way to do this is a bit of a hack, because razor doesn't support webforms view engine controls. However, it does support partial views made in the webforms View engine. So a hacky solution is to include a partial view with your control in it as such:

这样做的唯一方法是有点黑客,因为 razor 不支持 webforms 视图引擎控件。但是,它确实支持在 webforms 视图引擎中创建的部分视图。因此,一个 hacky 解决方案是包含一个局部视图,其中包含您的控件:

For example, say you want to use the office web ui ribbon in your mvc3 project, you could do so by including

例如,假设你想在你的 mvc3 项目中使用 office web uiribbon,你可以通过包含

<body>
    <div>
        @Html.Partial("_RibbonPartial")
    </div>
</body>

in your view. Where _Ribbon is of type .aspx

在你看来。其中 _Ribbon 的类型为 .aspx

then in your partial view simply use your control and set runat="server" and put it inside of a form with runat="server"

然后在您的部分视图中,只需使用您的控件并设置 runat="server" 并将其放入带有 runat="server" 的表单中

//_Ribbon.aspx
<form id="form1" runat="server">
    <XyzControls:Manager ID="Manager1" runat="server" UITheme="Silver" />
    <XyzControls:OfficeRibbon ID="OfficeRibbon1" runat="server" ApplicationMenuColor="#267c29"
        ApplicationMenuText="Item" ApplicationMenuType="Backstage">
//... rest of control code
</form>

Then you need to use ajax to implement events instead of using postback.

那么就需要使用ajax来实现事件,而不是使用postback。

Then to cleanup, get rid of the generated code from webforms view engine for post back that you don't need.. You can try keeping it, I haven't so I'm not sure what will happen. I know there are ways to have a fake viewstate as well if you really want to get into messy hacky stuff, but to remove the extra code from webforms you can use the following jquery:

然后进行清理,从 webforms 视图引擎中删除生成的代码,以便回发您不需要的..您可以尝试保留它,我还没有,所以我不确定会发生什么。我知道如果你真的想进入凌乱的hacky东西,也有办法拥有一个假的viewstate,但要从webforms中删除额外的代码,你可以使用以下jquery:

$(function ()
{
    // we don't need any of the webforms stuff
    $("#__EVENTTARGET","#aspnetForm").parents("div:first").remove();
    $("#__EVENTVALIDATION","#aspnetForm").parents("div:first").remove();
});

回答by Jesper Jensen

I had the same demand. Wanted to use custom webcontrol from Razor/MVC page. One should not do that with controls, that is handling postback. You don't have the eventcycle to support that, but if your only demand is to use a 'rendering control', you could instantiate it in razor, and control where the rendering takes place:

我也有同样的需求。想从 Razor/MVC 页面使用自定义 webcontrol。人们不应该对控件这样做,即处理回发。您没有事件周期来支持它,但如果您唯一的需求是使用“渲染控件”,您可以在 razor 中实例化它,并控制渲染发生的位置:

@{
  var myControl = new mycontrols.something();
  myControl.myattribute = Model.MyVar;
  mycontrol.RenderControl(new HtmlTextWriter(this.Output));
}