简体   繁体   中英

Asp.net Server Control alternative in MVC

我在Asp.net服务器控件项目中创建登录控件,并在Asp.net Web窗体项目中使用它,如何在MVC项目中做到这一点?

ASP.NET MVC framework doesn't really support the concept of server-side controls. However, there are a couple of approaches you can consider for abstracting view/logic into a reusable component:

Custom HTML Helpers

You can create custom HTML helpers, which is a good way to abstract server-side logic and rendering into a "black-box", similar to a custom control. This provides the developer with an server-side API to create, or instantiate, the "control". Take a look at this article for more information: http://www.asp.net/mvc/tutorials/older-versions/views/creating-custom-html-helpers-cs

Partial Views

Partial views allow you to reuse a chunk of Razor (if that's the view type you're using).

There are two typical ways to include a partial view within another view:

  • Html.RenderPartial - similar to a "include"; renders a view by passing a model to it
  • Html.RenderAction - executes the entire MVC lifecycle for the partial view; you specify a controller and an action, and the controller action returns a PartialView like so: return PartialView("partialName", vm); . Returning a partial from a controller action is particularly useful for return HTML fragments via an AJAX request for dynamic HTML rendering.

Here's another StackOverflow question that may help to clarify the difference between RenderPartial and RenderAction: RenderAction RenderPartial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM