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.<
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:
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!
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
__doPostback()
javascript function), orHowever, 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.
I believe you can use any control that does not rely on postback functionality.
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).
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.
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).