Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to implement user controls in ASP.NET MVC?

Like many others on this site I am considering a move to ASP.NET MVC for future projects. Currently my sites are running the traditional ASP.NET 2.0 Web Forms, and it works OK for us, so my other option is just to stick with what I know and make the move to ASP.NET 3.5 with the integrated AJAX stuff.

I'm wondering about how user controls work in ASP.NET MVC. We have tons of .ASCX controls, and a few composite controls. When I work with web designers it is very easy to get them to use ASCX controls effectively, even without any programming knowledge, so that's a definite plus. But then of course the downsides are the page life cycle, which can be maddening, and the fact that ASCX controls are hard to share between different projects. Composite controls are share-able, but basically a black box to a designer.

What's the model in ASP.NET MVC? Is there a way to create controls that solves the problems we've dealt with using ASCX and composite controls? Allowing easy access for web designers without having to worry about code being broken is an important consideration.

like image 755
Eric Z Beard Avatar asked Sep 03 '08 13:09

Eric Z Beard


People also ask

What is user control in ASP NET MVC?

An ASP.NET Web user control is similar to a complete ASP.NET Web page (. aspx file), with both a user interface page and code. You create the user control in much the same way you create an ASP.NET page and then add the markup and child controls that you need.

How do I run a user control in asp net?

To use a User Control in an . aspx we need to register it using the Register page directive, the register page directive has the following properties as: Assembly: This is an optional property used to register the assembly, for example Ajax control toolkit.

What controller should do in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.


2 Answers

To implement a user control you do the following call:

<% Html.RenderPartial("~/Views/Shared/MyControl.ascx", {data model object}) %>

You may also see the older syntax which as of PR5 is not valid anymore

<%= Html.RenderUserControl("~/Views/Shared/MyControl.ascx", {data model object}) %>

You will always have to worry about code breaking when moving from Web Forms to MVC, however the ASP.NET MVC team has done a great job to minimize the problems.

like image 157
Nick Berardi Avatar answered Sep 20 '22 21:09

Nick Berardi


As Nick suggested, you will indeed be able to render your user controls, but obviously the page-cycle, pagestate and postback from traditional ASP Webforms won't work anymore, thus making your controls most likely useless.

I think you'll have to rewrite most of your complex controls to port your website to MVC, while simple controls which, for instance, provide only formatting and have no postback status, should simply work. The code provided by Nick will simply work in this case.

And about sharing between more projects: I think controls will be more like "reusable HTML-rendering components" that can be shared across a website, rather than "reusable code components" with logic (like WebForms controls). Your web logic will/should be in the pages controllers and not in the HTML controls. Therefore sharing controls across more projects won't be so useful as in the WebForms case.

like image 38
LorenzCK Avatar answered Sep 21 '22 21:09

LorenzCK