C# 如何在 ASP.NET MVC 中创建可重用控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18567522/
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-08-10 12:37:00  来源:igfitidea点击:

How to create reusable control in ASP.NET MVC

c#asp.net-mvcasp.net-mvc-3razorcontrols

提问by Fanda

How can/should I create some "custom control" in ASP.NET MVC 3? I have red about partial views, ViewUsersControl, Html.RenderAction, but I still don't know, which way is the proper MVC way for razor views.

我可以/应该如何在 ASP.NET MVC 3 中创建一些“自定义控件”?我有关于部分视图的红色,ViewUsersControl, Html.RenderAction,但我仍然不知道,哪种方式是剃刀视图的正确 MVC 方式。

If I need to render some ajax component to view, I can imagine to do it by partial view, but what if I want to render section with custom logic?

如果我需要渲染一些ajax组件来查看,我可以想象通过部分视图来完成,但是如果我想用自定义逻辑渲染部分怎么办?

采纳答案by syned

1) PartialViews

1)局部视图

2) Custom Html helpers

2)自定义 Html 助手

3) Child Actions

3) 子动作

Update ASP.NET Core:

更新 ASP.NET 核心:

2) Tag Helpersare preferred way over Custom Html helpers

2)标签助手比自定义 Html 助手更受欢迎

3) View Componentsare used instead of Child Actions

3) 使用视图组件代替子动作

回答by Giannis Paraskevopoulos

You may use

您可以使用

@{Html.RenderPartial("YourCustomView",YourModel);}

For instance, In your Sharedfolder create a new Viewand name it "_MyCustomControl"

例如,在您的Shared文件夹中创建一个新的View并将其命名"_MyCustomControl"

Then in the code write :

然后在代码中写:

@model YourNameSpace.Models.YourModel

@{
    Layout = null;
}

@*Here write your control's markup*@

Then in your Views where you want to use this "control"you add:

然后在您要使用它的视图中"control"添加:

@{Html.RenderPartial("_MyCustomControl",new YourModel { args });}

If you get into trouble with RenderPartialcheck this out

如果你在检查这个时遇到麻烦RenderPartial

回答by Nipun Ambastha

As you have mentioned that you can use Partial Views. Yes you can use Partial Views, which is the most effective and efficient way.

正如您所提到的,您可以使用局部视图。是的,您可以使用局部视图,这是最有效和最高效的方法。

For Ajax rendering you can always use

对于 Ajax 渲染,您始终可以使用

     @using (Ajax.BeginForm("Details", new { id = Model.Post.PostId }, new AjaxOptions    
  {

Few of the links you would like to see

您希望看到的链接很少

Rendering partial view in ASP.Net MVC3 Razor using Ajax

使用 Ajax 在 ASP.Net MVC3 Razor 中呈现局部视图

Render Partial Views using JQuery in MVC3

在 MVC3 中使用 JQuery 呈现部分视图

回答by Adrian Thompson Phillips

In addition to all the other answers for this post, I can offer you the use of EditorFor and DisplayFor templates. These are useful when you want to easily render or edit a custom type. It'll handle validation nicely (which can get weird when using partials) and you can nest them recursively (again another feature that isn't obviously handy until you need it).

除了这篇文章的所有其他答案之外,我还可以为您提供 EditorFor 和 DisplayFor 模板的使用。当您想要轻松渲染或编辑自定义类型时,这些非常有用。它将很好地处理验证(使用部分时可能会变得奇怪)并且您可以递归地嵌套它们(这又是另一个在您需要之前显然不方便的功能)。

You can also use Html.RenderAction()or Html.Action()to call another controller action within a view and display the results in-line in the page. This is probably the closest to what you need as it allows you to render a partial, include code in the controller and it also allows for the passing of parameters.

您还可以使用Html.RenderAction()Html.Action()在视图中调用另一个控制器操作并在页面中内嵌显示结果。这可能是最接近您需要的,因为它允许您渲染部分,在控制器中包含代码,并且还允许传递参数。

Links to:

链接到:

DisplayFor and EditorFor Templates

DisplayFor 和 EditorFor 模板

Action and RenderAction

动作和渲染动作