Yesterday, I spent most of my day studying the MVC Framework Preview 3. To drive my research, I decided to try to redevelop a web site I previously wrote in ASP.NET WebForms. Because my web application is similar to the sample project of ScottGu (with products and categories), for the sake of brevity, I will keep his sample.

The intent of this blog post is to help new comers to have a quicker start, by exposing the difficulties I overcame yesterday.

Download link

First you need to install MVC Framework: http://go.microsoft.com/?LinkID=8955108

Using Html.ActionLink()

The Html class provides several handy helper functions that are a (very) light replacement to the Asp.Net controls.

One of them, ActionLink() builds a hyperlink, based on the given parameters.
In the Global.asax file, I have the following Route defined:

image

To be able to get a edit link on a product that points to http://localhost:56181/Products/Edit/5, we can useimage

Remark: in the third argument of ActionLink(), the name of the properties of the anonymous type should match the names used in the route (defined in the Global.asax).

For example, if I’d write:image

I would get the following link: http://localhost:56181/Products/Edit?productId=5

Cannot access a disposed object

I am using Linq to Sql to communicate with the database. To implement the List() action for the categories, I first wrote something like:

public ActionResult List()
{
    using (var db = new ProductsDataContext())
    {
        List<Category> list = db.Categories.ToList();
        return View(list);
    }
}

This works fine… until you call category.Products in the view. You would get the too famous Linq to Sql error “Cannot access a disposed object” because Lin q to Sql is trying to access the data context to fetch from the database the products of the category.

I first thought the view would be resolved during the “return View(list)”, before the data context is resolved. It seems it is done outside the List() method. So the easy workaround I found for this specific case was to use DataLoadOptions to load the related products of the category at the same time.

public ActionResult List()
{
    List<Category> list;
    using (var db = new ProductsDataContext())
    {
        var dlo = new DataLoadOptions();
        dlo.LoadWith<Category>(x => x.Products);
        db.LoadOptions = dlo;

        list = db.Categories.ToList();
    }
    return View(list);
}

(related external link)

How to submit a form?

To create a “edit product” page, like ScottGu’s example, we can use the Html class and the ViewData property:image

Remark: although the view Edit.aspx is called by giving a product id in the url, like “/Products/Edit/5”, I didn’t find an easy way to retrieve the parameter value from the view to use it in the form. the workaround is to put the product id in the ViewData in the controller class.

As we can see in the last screenshot, on submit the form will call the Save action. The Save action doesn’t require a view, we can save the data in the database from the controller and redirect to another action calling RedirectToAction:

return RedirectToAction("List", "Products");

Remark: I got some blank pages without explicit errors because I forgot to give a return parameter to the action methods in my controller. Do not forget to return a ActionResult in your action methods (RedirectToAction returns a RedirectToRouteResult object, this is a valid ActionResult to return)

Passing parameters to View UserControls

First, the following page is an interesting article about MVC compatible UserControls: link

To be able to pass more complex objects than strings to a view user controls, I used the Html.RenderUserControl() function, passing the data in the second argument:

image

Conclusion: when can I use MVC Framework?

Pros

  • Extensibility
  • Unit test friendly
  • Better control over produced html
  • SEO friendly (friendly urls for example)

Cons

(find more with question asked in forum)

At the moment, the MVC framework would be a risky choice for most of the projects I work on.

But because of the excellent extensibility of the MVC Framework, we can expect that the number of cons will be reduced over time thanks to the contributions of the community.