views:

909

answers:

6

Are there some things I cannot do with ASP.NET MVC? Things that are only possible with ASP.NET WebForms, or extremely much easier with WebForms?

We consider using ASP.NET MVC for a new project. But I wonder if there are some obvious things we will not be able to do with ASP.NET MVC when compared to WebForms, or places where we will have to spend a lot of time with ASP.NET MVC.

A: 

I think view-state is non existent in MVC. You will have to track your own view state in some other way than the built in view-state in non MVC projects.

EDIT: According to comments: "Getting rid of ViewState is an advantage not a disadvantage". – Craig

Roberto Sebestyen
MVC has a ViewData key/value collection that persists over posts for maintaining the state. a TempData collection is used for one-off state changes too: eg. status messages "this record was deleted"
cottsak
ViewState is part of the WebForms postback model and has no use in MVC, the same way it is not used with PHP or Python. Getting rid of ViewState is an advantage not a disadvantage.
Craig
+1 for Craig. Viewstate is a best just a mediocre thing, and in the worst situations it's a complete abomination. It's better we get rid of it.
Kibbee
Thanks for correcting me guys :-)
Roberto Sebestyen
+1  A: 

The big one is Controls. User Controls are not available with ASP.NET MVC. I have even gone so far as to try using code like this:

new Label().RenderControl(...ResponseStream...);

No dice.

Of course, as a part of that, there is no need for view state, so that isn't in there.

Server controls do work, though.

JoshJordan
While true, you can use partial views for something similar
Marc Gravell
this is by design: the MVC approach attempts to create simplicity and greater control over precisely what markup is rendered
cottsak
MVC does have User Controls, it doesn't have Server Controls as they are designed to go with the WebForms model. Because MVC is not meant to be visual drag'n'drop, event driven type development Server Controls are not required. It is not an oversight, it is a design decision.
Craig
Drag and drop controls are called User Controls. Server Controls are controls that you write the RenderControl method for.I never said it wasn't a design decision, but it wouldn't be right to state just that, because they are planning to include user controls in a future release.
JoshJordan
User Controls are .ascx files which are already supported as Partials. asp:Textbox, etc are Server Controls and you can write your own with designer support.
John Sheehan
@Craig ...it is a feature! http://files.myopera.com/freejerk/files/bug-feature.jpg ...kidding :)
Arnis L.
+11  A: 

The biggest one would be using existing 3rd party controls on your form. Most of the inbuilt controls are pretty easy to reproduce, but if you have a pet 3rd party control, you might have to host that on a regular (non-MVC) aspx page (luckliy this is supported).

Likewise, "web parts"

Also - the feature where ASP.NET uses different html for different clients (mobile, etc) becomes... different; you wouldn't want to do this by hand, but in reality most clients now work with standard html, so it is less of an issue in the first place.

Some things like i18n via resx files need extra work than is in the vanilla MVC template, but the samples are there on the internet.

One point... MVC is licensed only for MS/ASP.NET; so one thing you can't do (without violating the terms, as I understand it) is to run it in mono/Apache - but IANAL.

Now consider the things you can do with MVC, that you can't (or are hard) with vanilla:

  • routes instead of pages
  • automated input resolution (action arguments)
  • proper html control...
  • ...enabling jQuery etc for simple AJAX
  • separation of concerns
  • testability
  • IoC/DI
  • multiple templating options (not just aspx/ascx)


re input resolution:

public ActionResult Show(string name, int? page, int? pageSize) {...}

will pick "name", "page" and "pageSize" off (any of) the route, query-string or form - so you don't have to spend lots of time picking out request values.

re templates - aspx/ascx aren't the only templating options. For example, see here; or you can write your own if you like... The view is not tied to ASP.NET controls at all.

Marc Gravell
Thanks for the nice reply, which makes me more sure that it's relatively safe to go for mvc. However, I don't understand what's behind your points with 'automated input resolution', 'IoC/DI', and 'multiple templating options'. Have you got some examples of what you mean by that?
Ole Lynge
updated per question
Marc Gravell
ASP.Net MVC is open source under the MS-PL, which is a very liberal license, see http://weblogs.asp.net/scottgu/archive/2009/04/01/asp-net-mvc-1-0.aspx. It's perfectly legal to run under mono.
jeroenh
A: 

As Marc said, third party tools and (serverside) webcontrols cannot be used. So slapping something together quickly by dragging and dropping a few controls on a form (like a grid and a dataaccess control) is no longer an option.

But with the codegeneration etc. you can still make something quickly. And you still have the above option if you need something quick.

Morph
+2  A: 

Validation is not as easy as in WebForms. In Webforms you can add a validator and just set a property that enables clientside validation. You can localize the errormessage. The localization works clientside and serverside.

There is no out of the box clientside validation in MVC and you need to find a way to localize clientside errormessages.

Localization itself is different. Ressources obviously by default dont exist per page, because there is no page. But there is a good way to have ressources per view.

I still did not check, if it is possible to set SSL-required per folder.

Malcolm Frexner
Thanks for the comments. Concerning serverside/clientside validation, I found xval (http://xval.codeplex.com) which seems to offer a solution to that...
Ole Lynge
Validation has been much improved in MVC2
Richard Ev
A: 

ASP.NET Ajax is not working with ASP.NET MVC so no UpdatePanel (due to lack of postback). Fortunately there is built-in ajax (Ajax.Form) which can be used to perform partial updates not to mention jQuery which is shipped by default with the Visual Studio project template.

korchev