ASP.Net MVC Framework and data binding.
Note: The content in this blog post is based on the an early prototype of the ASP.Net MVC Framework and stuff can or will change.
I got a question if it’s possible to use data binding with the ASP.Net MVC Framework. Sort of it’s possible, at least to display data but not to do a two-way data binding with existing controls. The reason of that is because it’s not implemented yet ;) The way the data-bound controls work today with data-binding will not work with the MVC Framework, but we will probably see a solution to solve this in the future. When we do a POST and post data a controller’s action method will be invoked. This method can take some arguments as input parameters, for example:
/Products/Edit/1 /Products/Edit?id=1
[ControllerAction]
public void Edit(int? id)
{
}
Note: The URL is routed to the controller, and invokes an action method.
We can’t from the control get access to the controls by using an id as in a normal postback scenario. What we can do is to use the Request property of the base class of our controller (If we decide to inherit the Controller class). The Request property is of type IHttpRequest. The reason why it uses an interface is because we should easy mock out the Request to make sure we can simply do unit-testing without needing to run our controllers within ASP.Net. By using the Request property we can simply get the data from our input fields and map them to our model:
<input type=”text” name=”Description” …>
product.Description = Request[“Description”]
In an early prototype from Scott Guthrie, he used an Extension method that could take the Request.Form as an argument and do the mapping between user input fields and model.
product.UpdateFrom(Request.Form)
In the view he used a naming convention to easy see which user input field should be mapped to which property of the model:
<input name=”Product.Description” …>
There are other different prototypes to make it easy to fill a model out from the posted data.
We can use some of the controls to do one-way data binding to display data. Scott Guthrie has a good example in his post about how we can bind data to the ListView control that is shipped with ASP.Net 3.5 (ASP.Net 3.5 will be available at the end of this month, so really soon).
If we have a ListView control in our View we can bind data like this:
<asp:ListView id=”productList” runat=”server”>
<LayoutTemplate>
<table>
<asp:PlaceHolder Id=”itemPlaceHolder” runat=”server”/>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Eval(“Description”) %></td>
</tr>
</ItemTemplate>
</asp:ListView>
Because the View is responsible to render the model, we will do the data binding in the code-behind of the View.
public partial class Products : ViewPage<List<Product>>
{
public void Page_Load()
{
productList.DataSource = ViewData;
productList.DataBind();
}
}
Note: Because the View inherits the ViewPage<T>, we can specify the type of the ViewData (ViewData will be of type T). The ViewData is a property of the ViewPage which will contain the data we passed to the RenderView method in our control:
[ControllerAction]
public void List()
{
List<Products> products = ProductRepository.GetAll();
RenderView(“Products”, products);
}
As you have seen in this post, we can do one-way data binding, but at the moment we can't easy do a two-way data binding. You will see more post about the MVC Framework from me.