Tuesday, 31 March 2009

ASP.Net MVC Resources

I’ve been working recently on a project using the ASP.Net MVC framework – Microsoft’s implementation of this pattern as an alternative means of presentation from Web forms. There are a number of basic tutorials on the web, but it’s taken a bit of digging to make progress beyond the relatively simple examples.

As with any new technology, you get stuck on things that should be simple, but having worked through these issues am enjoying using the framework, and have started to use it for an, albeit small, production project for a client.

The project follows the standard list, create, edit, delete application type that many of the tutorials follow, so it seemed a good fit.

In this post I’ll pull together some of the best resources I found that helped me on my way to developing this application, along with some tips on how certain problems were solved.

Tutorials

Firstly the application was based on the excellent tutorial Stephen Walther has released – this demonstrates the use of views and controllers, and extends the model to provide data access and validation, via clear use of interfaces and design patterns to adhere to the loosely coupled best practices in this area. He also illustrates how MVC supports unit testing – one of its major advantages over web forms.

Taking Things Further...

Editing Complex Properties
One of the first hurdles I had to get over was the editing of a record that has a field selected from a drop-down list (as opposed to simply entering the value in a text box that most of the simple tutorials demonstrate – these work “out of the box” if you make sure you use a typed view and ensure the names of your input fields match the names of your class properties).

There are three discrete challenges here – one is passing the data to the view that populates the selection list, another is pre-selecting the current value and finally we need to get the updated value to pass up to the data access layer.

The following controller logic passes additional data to the view for creating and editing a list of users for an application; in this case a list of user "roles" to be selected in a drop-down list. The Create action passes the list, the Edit the list within an additional parameter indicating the current selection:

public ActionResult Create()
{
GetRoleListForViewData();
return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")] User objUserToCreate)
{
//Get selected Role object and attach to created User
objUserToCreate.Role = mobjService.GetRole(byte.Parse(Request["Roles"]));

//Create user record - redirect to list if succcesful or redisplay form if not
if (mobjService.CreateUser(objUserToCreate))
return RedirectToAction("Index");
GetRoleListForViewData();
return View("Create");
}

public ActionResult Edit(int id)
{
User objUser = mobjService.GetUser(id); //retrieves User object
GetRoleListForViewData(objUser.Role.ID);
return View(objUser);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(User objUserToEdit)
{
//Note - the following does not work when using Entity Framework
//it marks the object as "Added" rather than "Detached"
//and leads to problems when calling ApplyPropertyValues
//
//objUserToEdit.Role = mobjService.GetRole(byte.Parse(Request["Roles"]));

objUserToEdit.Role = new Role();
objUserToEdit.Role.ID = byte.Parse(Request["Roles"];

if (mobjService.EditUser(objUserToEdit))
return RedirectToAction("Index");
GetRoleListForViewData(objUserToEdit.Role.ID);
return View("Edit", objUserToEdit);
}

private void GetRoleListForViewData()
{
ViewData["Roles"] = new SelectList(mobjService.ListRoles(), "ID", "Name");
}

private void GetRoleListForViewData(int intSelectedID)
{
ViewData["Roles"] = new SelectList(mobjService.ListRoles(), "ID", "Name", intSelectedID);
}

Useful Code Samples

Paging
Martijn Boland provides an excellent sample project that can simply be referenced in order to provide a paging mechanic.

No comments:

Post a Comment