are you still creating complex user controls in asp.net MVC? - asp.net-mvc

Is there a new model or best-practise for creating complex controls in asp.net MVC?
Do you use code-behind or inline to mirror your view pages?

My model is this:
I tend to use Partial Views when there is a view element that I'll need to use more than once. Or if I need to display multiple complex object in a view.
I use RenderAction from the futures assembly when I need a "reusable widget" of sorts. It has it's own controller and is better at handling more complex logic than a Partial View.
Finally, I tend to write Html Helper methods for things I may use in other projects (like paging links, etc).

I would use a partial view for complex things. Check out this article

Controls in MVC don't generally have (any) code behind. You use PartialViews as ascx controls, you pass them a model and you display the contents of the model.
You can create custom controls in mvc and these compile to a dll which is moveable between projects etc and these are a little more complex but essentially they spit out html like the partial view does.
You can also create jQuery plugins that are pretty cool and again, they can spit out html based on a model.
So a typical mvc view may be comprised of several partial views each of which are dedicated to a model or hierarchy of models.
Partial views can also display partial views so you can send a complex model to a partial view which in turn renders other partial views each of which deal with a more atomic part of your model.

I've been writing a lot of my own HtmlHelper extension functions. The November meeting of http://www.c4mvc.net/ that was recorded today gives some great examples of control type code placed in HtmlHelper extension functions. The recording should be online soon.
You may also want to check out the Telerik Extensions for ASP.NET MVC. They're open source, so even if you don't use their controls, you can get some insight into controls in ASP.NET MVC by taking a look at how a commercial control vendor approached the problem.

Coming from a PHP turned webforms turned ASP.NET MVC background, I find myself relying a lot more on basic html/css/javascript.
I've never been a fan of controls, even with webforms because they always ended up being messy compared to js/html/css counterparts.

Related

User Control Equivalent in ASP.NET MVC 4

I'm working on a new ASP.NET MVC 4 app. I'm really trying to do things the "MVC" way. In particular, I'm interested in using the Razor view engine. Please note that I come from a web forms background, so my questions may seem awkward.
I'm trying to learn if there is something equivalent to user controls in MVC? If so, what are they called? If not, what is the recommended way to creating a reusable component in MVC?
You're looking for #Html.Action and #Html.Partial.
They allow you to either render a partial view or a full blown controller action in a view. This is a common pattern of reuse in MVC land.
Occasionally you would want to make displayFor or editorFor templates, if a controller action is too heavy. The rule of thumb is if you need to do it multiple times on the page and it needs to be posted back in a form, think about doing it in a template.
Controls in asp.net cover a rather large swath which MVC granularizes a bit more.
To creating a reusable HTML component in MVC you can create a partial view in the Views/Shared folder and use #HTML.Partial("_PartialViewName") to include it in any other view or partial view. You can find out more about partial views in this article.
You could try:
Add a View Start _ViewStart.cshtml in Shared folder
Make Layout Null (if required) #{ Layout = null; }
Call this page where you want like #Html.Partial("_ViewStart")

MVC replacement for web form user controls (ASCX)

I'm quiet newbie to ASP.NET MVC world. In Web Form we can write a user control and encapsulate all the details within. We can then reuse the control on N number of pages which is a good code-reuse.
I would like to do the same in MVC 3/4 and haven't had any lucks. Could someone please help me with how above can be achieved?
Thank you.
Probably the closest thing to an ASCX in MVC is a RenderAction.
Similarly to a user control, you can have your partial view binded to a specific action that is independent from the container view and it's action.
There is no exact equivilent. There are, however, ways to reuse code. You can create HtmlHelper extensions (like the Html.Whatever() methods), Or you can use Partial Views, or you can use templates. All of these do different things, but they offer various ways to reuse code.

ASP.NET MVC Custom Controls

Is it safe to say that custom controls with ASP.NET MVC are most times just partial views? And if that's the case, I'm guessing it's always up to the implementing application to dictate the behavior (through controller code) of these controls?
I have done a bit of searching, and there is almost no resources on ASP.NET MVC custom controls (either that, or I'm missing the mark with my Google skills).
Partial views are more a template for either a control or a set of controls that can be shared between views.
I'm not sure they are "Custom Controls" as such and it sounds like you are coming from an ASP viewpoint.
I think you need to first get in the mindset of MVC and out of ASP.
So for a list of items you may have a partial view that takes the list of items and a partial view that takes an actual item. So "pvCustomerList<List<customer>>" and "pvCustomer<customer>".
The pcCustomerList iterates through the list and creates a pvCustomer for each customer in the list.
Partial Views don't really have code in the controller. Instead they are passed data from the view. If there is a submit action in the partial view, then this is either handled by the controller for the view or a jQuery post back.
I hope this clears things up a little for you.
Have you checked out NerdDinner sample?
There is nice article on asp.net mvc website regarding form helpers (helper methods for views). there is explained how to create custom helper method
Link is http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

Does the concept of a Control make sense in MVC?

I've started using MVC reccently, and one thing that occurs to me is whether its possible for the concept of a Control to exist in MVC?
From what I see the framework allows the application to be nicely factored into Models, Views and Controllers, but I can't think of a nice way to take a "vertical slice" of that application and reuse it in another application.
What I mean by that is it strikes me that any UI components I build with MVC maybe not very amenable to reuse, in the same way you can reuse a Contol in ASP.NET WebForms. Ok, there are HTML Helpers but I am thinking of something more modular. Something more like the control model in WPF.
Does this dichotomy go to the heart of MVC vs WebForms, or can reusable UI components work in an MVC world?
You can still use ascx files and other features in ASP.NET Web Forms. The only missing piece in MVC is the postback model and view state oriented state management. There is no <form runat="server"> anymore and no automatically generated hidden fields. Except these, all other features can be used in ASP.NET MVC. You can still write controls that post data using a REST based mechanism and use them in your views.
So, yes, as long as your controls don't rely on a server side form for postback, you can use them exactly the same way you'd use in ASP.NET Web Forms.
I was looking for the same thing - for reusable widgets with their own data paths and found this on Steve Sanderson's Blog:
http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/
From the article:
"You’ve heard of partial views, so how about partial requests? Within any MVC request, you can set up a collection of internal partial requests, each of which can set up its own internal partial requests and so on. Each partial request renders a plain old action method in any of your plain regular controllers, and each can produce an independent widget."
This article respectfully offers a alternative to The MVC Contrib Group's Sub Controller strategy (http://www.mvccontrib.org/) which is also a solution for what you are looking for.
I guess its harder in MVC to completely encapsulate rendering and post back handling in a single control you just drop on a page. It some ways this is because ASP.NET Webforms is very abstracted from HTTP semantics and that abstraction is a framework where its possible to create reusable user controls.
ASP.NET MVC doesn't have the abstraction of webforms so you can have a control the posts to three different controllers. While it may feel like your losing easy to use controls when you move to ASP.NET MVC, I think you have a better framework for separating and reusing domain logic.
In ASP.NET MVC you have partial views which can be reused. Rob Conery has a good post on this: ASP.NET MVC: Using UserControls Usefully
I think the closest thing you'll get to an old school controls are partial views, these are effectively just shared markup that you can drop on any page. However they don't have their own controllers out of the box so the code to power the shared UI component (the partial view) would need to exist in the controller of every page that used it. There are ways to reduce the duplication of code but without implementing partial view controllers and binding I don't think there is a way around it completely.

What place for server controls in ASP.Net MVC?

What is the recommended replacement of ASP.Net server controls in the bright new world of ASP.Net MVC?
In my opinion, one of the best features of ASP.Net is the ability to write server controls (although, admittedly, the event model is horrendous to deal with). If these controls are self-populating, then they can be shared between different projects with the minimum of fuss - you simply reference the assembly where the server control lives, and drop it on to the aspx. The control does the rest. This fits very nicely in the World of Widgets and provides efficient code reuse. How is one meant to achieve the same thing in MVC?
I am most interested in self-populating controls that do not post back, as I appreciate that the postback model definitely does not fit with MVC. Can they still be encapsulated in a class that can be shared between a number of different MVC web projects? Or does this require a whole different mindset where controls shouldn't populate themselves, and one should use partial views? Is there a way of sharing partial views between projects?
Finally, can I use my old (non-postback) server controls, in an MVC projects?
You can mimic the behavior of non-post back controls with Html helper extension methods. Just like Html.TextBox(), etc, you can write your own and encapsulate them in their own project if you like.
If you've written controls that just output HTML, it shouldn't be that hard to convert them to Html helpers.
The closest Asp.Net MVC comes to server controls is partial requests. In a partial request an MVC action method is called, and its output is appended to the current view. Unfortunately, the official support for this (Html.RenderAction) is in the futures assembly at the moment.
If using the futures assembly is not possible for you, a blogger named Steve Sanderson has written an article on implementing similar functionality:
http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/

Resources