How to create reusable control in ASP.NET MVC

后端 未结 4 1329
有刺的猬
有刺的猬 2021-02-03 12:42

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\

4条回答
  •  悲哀的现实
    2021-02-03 13:17

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

    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.

    Links to:

    DisplayFor and EditorFor Templates

    Action and RenderAction

提交回复
热议问题