Luis and David recently posted about the controls that appear in the ASP.NET MVC Futures 1.0 release.
I’ve seen some discussions where people positively erupt at any mention of the word “control” in an MVC setting. These are the people who consider ASP.NET Web Forms as the ultimate source of evil in the universe – a cross between a Sith lord and a velociraptor. The idea of introducing controls, with un-testable event handling code and giant gobs of view state, is a travesty that will corrupt the minds of all developers before devouring their children. They must be stopped.
Let’s take the simple scenario of rendering a text input in the browser. If you are using the ASP.NET MVC framework release with no additional libraries, you’ll be looking to use one of the 4 Html helper TextBox methods (excerpted for space below):
string TextBox(... string name); string TextBox(... string name, object value); string TextBox(... string name, object value, IDictionary<string, object> htmlAttributes); string TextBox(... string name, object value, object htmlAttributes);
A text input is a pretty trivial control, but even these 4 options don’t give you a clean path to the entire universe of things you might want to do when displaying a value in a text input, like specifying a formatting string to use when converting the model value to a string. One solution is to write additional helper methods, or include an additional library with the methods already written, but you are only adding to the number of method overloads a developer has to parse when they just want to display a simple text input. In the end, you’ll still be looking at code similar to the following…
<%= Html.TextBox("ReleaseDate", String.Format("{0:d}", Model.ReleaseDate), new { @class = "special" })%>
…versus the declarative syntax of the MVC futures TextBox control…
<mvc:TextBox Name="ReleaseDate" Format="{0:d}" class="special" runat="server" />
Note that the MVC control does not inject view state into the rendered output, but that’s not to say that the MVC controls don’t have some issues. One issue is that they still inherit properties, behaviors, and events from System.Web.UI.Control, and some of those inherited features don’t make sense in an MVC view.
I think I'm still happier using Html helpers.
Here are some advantages I see to using controls:
And the disadvantages:
It’s also worth pointing out that there are other solutions, besides controls, to the often clumsy Html helpers. By sticking to view-specific models, conventions, and effective CSS with JavaScript, you can remove many of the concerns that the HtmlHelpers burden themselves with. Using a view engine other than the web forms view engine can also solve some of these issues.
What do you think?
Will MVC controls be the spawn of Satan, or the blessing of a saint?
Comments
It looks to me that the primary purpose of them is designability. But since I generally find the webforms designer to be mostly useless in any real app, that feature doesn't mean much to me.
Another possible solution to HtmlHelper overload hell is the fluent HTML builder interface like the one included in MvcContrib.
@Ales - This is a fantastic idea. Looks like you have some interesting blog posts. I'm trying to translate them now.
@Kev - Yep.
@Stan - Your wife has a skull on her rump?
The only scenario that I see that makes sense is if you want a control to emit its own script, etc at the HEAD of the page.
With MVC, you don't get anything you don't ask for, so this sort of goes against that experience. I don't mind so much anyway, since you have more control (you could combine/gzip the js files easily for example).
simle
<input type="text" name="ReleaseDate"
value ='<%=String.Format("{0:d}", Model.ReleaseDate)%>' class="special" />
not greatly more than control.
in Another ViewEngines is NO issue with head title and emit some css/js to head.
<%= new TextBox("ReleaseDate") { @class = "special", Format = "{0:d}" } %>
The problem is of course, getting the reference to the model.
Personally the declarative approach is much better, I don't see how either is more testable.. declarative or monolithic overload hell? hmm, what are we testing? and how is it inherantly flawed in a declarative system.
I think what most people mean is.. System.Web.UI.Control is evil, and yes - yes it is.
Even in a webforms environment I don't use any of the asp controls unless I absolutely have to...and then I throw up ever so slightly in my mouth.
-J
Is that because they data bind to a property via a string name?
On my current ASP.NET project, we are solving that particular problem by using lambda expressions rather than strings, to define the bindings. I wonder if there would be any way to achieve a similar result with MVC controls......?
The problem here is that the greatest thing about MVC is control over the markup. For that reason I generally don't use HtmlHelpers either except in specialized cases that don't affect "content", such as some custom extensions to manage script references.
I recently started using the Spark view engine, and all I can say is, oh my God, away with the WebForms view engine already!
Why are we even trying to work around the limitations of that view engine when there is such a better alternative out there already, which fits the MVC model way better? What MVC needs is "official" backing of a view engine that embraces HTML and is designed with MVC in mind.
Right now, the "official" MVC release is a big hack around the WebForms model.
If Microsoft is really backing MVC, I don't see why they would have any problem providing the MVC team with the WebForms view engine code so you can customize it.
What I would really like to see is a view engine that maintains ASP.NET binding syntax for familiarity, but adds the declarative Spark attributes and elements for condition testing and looping. That would be a big win in my books.
I am one of those who simply hate WebForms and love doing clean and beautiful HTML code. As a part of the solution I moved to MVC.
What I dislike about MVC is how the view can get really ugly.
Thanks to MikeM above, I just found out about the Spark View engine, which seems to solve many problems with the view!
But I do wonder what the future may look like for ASP.NET's MVC. People were sharing neat things such as a calendar control to each other by creating custom controls. How will this be done in MVC without any controls? I personally think there needs to be a new solution, outside the box.
I don't understand the control-haters out there. My users demand very complex grids. Sorting by clicking on the column header, scrollable rows with fixed column header, edit within the grid, etc. You want me to roll this from scratch?? And do it on every form with a grid? A ridiculous concept and my clients will never hire me again after my bill quadruples. But you say, "We want clean HTML!!!!" Yeah, well you also want a website with no functionality then.
Confused.
But, I have a real and stronger need to not re-invent the wheel with each new development effort. So I like and need controls. I also like MVC (not convinced about need yet).
If I have a client with a website that contains familiar and easy-to-use controls, I am going to think twice about dumping the control, just to go to MVC. I am going to think three times before I try to justify the cost (and their bill).
MVC capable of supporting controls (and somthing like events) is far preferable than MVC alone.
stackoverflow.com/.../jquery-grid-recommendations
Is that where MVC devs should look for so-called controls?
Bill