0

i create Login control in a Asp.net Server Control project and use it in Asp.net web form project how can i do this in MVC projects ?

mehrdad
  • 357
  • 2
  • 25

1 Answers1

1

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

Community
  • 1
  • 1
Joseph Gabriel
  • 8,339
  • 3
  • 39
  • 53
  • You can specify any amount of server-code as part of a custom HTML Helper. What type of control are you considering? That may make a difference whether it's more suited for a custom HTML helper or a partial view. – Joseph Gabriel Apr 07 '13 at 14:20
  • I want to create login form component to use in some MVC projects – mehrdad Apr 07 '13 at 14:55
  • Login component is often implemented as a controller/partial view. Any server-side processing logic that you have in your web forms project would be implemented in the login controller. Take a look at the default MVC project template in Visual Studio for a good example. – Joseph Gabriel Apr 07 '13 at 17:29