Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP .NET MVC correct UserControl architecture

I'm trying to learn the new ASP .NET MVC framework and would like to know the best practice for using UserControls.

I understand that you can render UserControl's as a partial and pass data to them from a controller. Ideally I would think that it makes sense not to have a code behind file as this creates a temptation to break the MVC rules.

I'll give an example where I don't understand how UserControls fit into the pattern.

I have a UserControl that shows the latest tags (much like on StackOverflow). Unlike StackOverflow I want to display this UserControl on all of my pages. If I have a controller say QuestionController which is meant to handle actions from some question views e.g. view and detail, does this mean I have to fetch the data in the QuestionController and then pass it to the UserControl?

If I create another controller say SearchController then I would have to replicate the same functionality to get the latest tags to pass to a partial again. Doesn't this mean that the 2 different controllers are doing extra things that they weren't originally intended to do?

like image 996
Alex Avatar asked Apr 01 '09 11:04

Alex


People also ask

What is Usercontrol in asp net?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.

Can we use user control in MVC?

In ASP.Net you need to register one tagprefix and then use the user control by tagprefix registered but in ASP.Net MVC using simple Thml. RenderPartial we can use the user control.


2 Answers

If your UserControl appears on every page, then one way to address this would be to use a base controller from which all of your controllers derive and generate the ViewData for the UserControl by overriding the OnActionExecuting method and putting the logic there. If your UserControl is less pervasive, but still frequently used throughout the site, you could extend ActionFilterAttribute and have your filter generate the needed data. This attribute could be used to decorate the controllers or actions that generate views that use the UserControl.

I'm assuming in all of this that the data for the UserControl is independent of the action being invoked. If there is a dependency, it's probably best to push the logic into a class (or classes, perhaps using Strategy) and make the generation of the data explicit in each action or controller (via overriding OnActionExecuting).

like image 190
tvanfosson Avatar answered Sep 25 '22 18:09

tvanfosson


Alternatively, with ASP.NET MVC 2 you can now use RenderAction to call a completely new controller action which can fetch the data. This makes your code much more modular and it is more clear where the data is coming from.

like image 33
mike nelson Avatar answered Sep 24 '22 18:09

mike nelson