views:

482

answers:

7

In ASP.NET MVC, do I use the "regular" controls from the toolbox and assign data to them like I did with webforms? Can I use the gridview, for example?

Thank you.

EDIT: The answer appears to be no. So what do you use? Are there any drag and drop controls in Visual Studio like the webform controls?

+1  A: 

I believe you can use any control that does not rely on postback functionality.

Derek Ekins
A: 

No, any control that uses the __doPostBack JavaScript function to trigger postbacks will not work. Also there is no concept of ViewState in ASP.NET MVC so any control that relies on ViewState or ControlState will not work either.

The idea behind ASP.NET MVC is to take a different approach than the heavier WebForms model (and most of its controls).

Andrew Hare
A: 

You cannot use WebForms Controls in ASP.NET MVC directly.
But you can wrap any control inside an HTML helper.

Example a GridView:

public static class MvcGrid
    {
        public static string MyGrid(this HtmlHelper helper)
        {
            var grid = new GridView();
            var source = new[]
                             {
                                 "Foo",
                                 "Bar",
                             };
            grid.DataSource = source;
            grid.DataBind();
            var stringWriter = new StringWriter();
            var writer = new HtmlTextWriter(stringWriter);
            grid.RenderControl(writer);

            return stringWriter.ToString();
        }
    }

But this will not function as a WebForm GridView.
It is just for rendering the HTML.

Marwan Aouida
A: 

Some controls from the WebForms toolbox will work just fine (although I believe the GridView is not one of them...) in ASP.NET MVC. The keys are that you can't use controls that rely on either

  • Postbacks (the __doPostback() javascript function), or
  • Viewstate

However, I'm not sure that you really want to use those controls. One of the main ideas with ASP.NET MVC is to regain control over your html markup - something you don't really have with a GridView. If you want to have a reusable grid layout to present data, I recommend using a PartialView or an Extension Method to the HtmlHelper class, depending on the complexity of your data.

Tomas Lycken
+2  A: 

ASP.Net MVC is focused around generating your own HTML using your View Templates. You can't use Webform controls in your Views and have the application handle them the same way as with Webforms. They might render properly, but there is no postback functionality or concept of maintaining their state.

That being said, you can integrate ASP.Net Webforms with your MVC style pages as well. You can always check out the classic Hanselman walkthrough of having a "hybrid" asp.net application: http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

womp
+2  A: 

For the most part, use of MVC is explicitly not a drag/drop affair. There isn't a level of auto-wiring data-bound controls either. The purpose within MVC's structure is to give more direct control over the markup. There are some helper methods that can be used with rendering Form controls. MVC is a very different development model server-side. NOTE, you can combine ASP.Net with MVC into a hybrid project.

I would suggest that you need to be very comfortable with HMTL markup, and how HTTP + HTML communication works in order to be successful with an MVC project. The use of javascript is also very explicit. MVC is very nice with regards to a separation of concerns from back end, and front end in web based applications. This makes it easier to design and test the back-end logic, in comparison to the front-end rendering. It is a framework for web developers (web based applications are the first concern), not a developer framework for a web front end (ASP.Net controls based).

Tracker1
This makes me wonder if they get a bigger development audience or lose some folks. This seems to be even with everyone else out there with an MVC framework other than visual studio as the IDE.
johnny
Also, doesn't this more things backwards in some ways?
johnny
Tracker1
A: 

The answer is Yes and No. Preferrably No.

This is what the community fears, an aggressive blending of asp.net and MVC into an unrecognizable and unecessarily complicated kludge.

For anyone looking at MVC, I'd suggest:

  • Appreciate clean simple HTML. Empty out your toolbox :-)
  • Surf and learn how to code controls to be generated the way you need them to. Strongly typed, powerful html generators will come but I'd prefer them to be clean and not wrapped in a control, I want to see the code.
  • try not to mix asp.net and mvc ( tempting ) but unless you're forced to, consider it a no-no

So to answer your question, MVC is still new, MVC Contrib, MVC Futures and Html helpers are avaiable in the framework and all over the web. Keep surfing and keep your own library of tweaks then post them so others can find and improve on them and vice versa. MVC can be the change the .net community has been waiting for, let's not go back to drag and drop so quickly.

Good luck!

5x1llz