ProfK
ProfK

Reputation: 51063

Server Controls and MVC

I've been lead to believe, without research, that server controls are forfeited in the MVC model. I am both very curious to know if this is true, and if so, how would one achieve something equivalent to a Repeater control, or any other view whose exact structure depends on the content of the model?

Upvotes: 3

Views: 1129

Answers (6)

terjetyl
terjetyl

Reputation: 9565

ASP.NET MVC does not support ViewState nor postbacks so all asp.net controls relying on that will not work. The repeater control is one of the few that doesn't need it so it does work though.

The mvc equivalent of control is the HtmlHelper classes which generate html for you. The collection of htmlhelpers is quite large but you will not automatically find equivalents to the regular webforms controls because of the different nature of webforms and asp.net mvc.

MVCContrib is the contrib project for asp.net mvc so you will also find more htmlhelpers there http://www.codeplex.com/MVCContrib

It is also quite easy to roll your own htmlhelper methods

A good starting point for ASP.NET MVC is found here http://www.asp.net/mvc/

Upvotes: 2

Hrvoje Hudo
Hrvoje Hudo

Reputation: 9024

I suppose that there will be some kind of smart helper methods, like html.textbox or similar, and they will draw table/grid for example, based on given object collection.
You can pack your html helper inside asp custom control and use it in any mvc app...
Some think like regular asp.net server side controls, but without viewstate, events, and all other pagelifecycle-dependent stuff.

Upvotes: 0

Kieron
Kieron

Reputation: 27107

That's incorrect, you can still use Server Controls. The engine that parses/ processes the views is still the same ASP.NET runtime that Web Forms uses.

An example using the Repeater control.

Upvotes: 0

Jordan Gray
Jordan Gray

Reputation:

I disagree, Yuriy, that controls aren't useful. A set of data-bound MVC controls, easily bound to viewdata, sans obstinate ID rewriting, with no reliance on nasty viewstate or postback gunk and complete control over templating (defaulting to sensible markup), would be an excellent addition to the MVC framework. Controls are a very powerful and useful concept which can significantly increase development time, and shouldn't be discarded solely because they carry some emotional baggage from the ugly old webforms days! :)

Upvotes: 1

Yuriy Ostapenko
Yuriy Ostapenko

Reputation: 61

Correct, rails-like MVC doesn't fully support server-side controls with all their messy viewstates etc... You CAN use some of them but shouldn't really even bother. You can reuse UI portions by utilizing view components and maybe use some nice client-side widgets from jQuery UI or Yahoo UI to build rich grids, etc... As for the repeater... "foreach" is your friend :)

Upvotes: 1

Bryan A
Bryan A

Reputation: 3634

As far as I've understood, you don't need server controls. Instead, you would build a model class, and then a view class that would be responsible for displaying the data from that model. Excerpt from MSDN:

Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

View. The view manages the display of information.

Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

Upvotes: 0

Related Questions