Archive for February, 2010
Editable Grid / List Binding in MVC2
Feb 27th
Steve Sanderson has a great post on how to perform list binding with MVC2 and custom HTML Prefix Scoping. However, it did not demo the out of box functionality that MVC2 provides, so I’m going to do that here. Please note, I’m using MVC2 RC2 in this example. At the end if this post you will be able to download the solution and run/debut it on your machine.
The App
I’m going to be using Steves sample app. When you first first fire it up you will be presented with a link to go to Donn’s list binding demo. This demo emulates using the built in features of MVC2 to great an editable grid or list. It will look like this:
After clicking on that link you will be presented with a grid / list that is editable. I’m going to use the EditorFor() expression based input builder and some custom editor for templates to layout the grid. In the end, the grid will look like this (yes, not pretty, but it demonstrates what can be done with some of the editor templates.
This is a order list that is set up in the controller manually. The Controller invokes the index action with a customer view model. The customer object has a list of orders, and each order has a list of line items. The objects look like this:
Customer
Order
LineItem
Solution Layout
The solution is lain out in a way that we can render the Index.aspx, which will render an editor for the order. The OrderRow.ascx file is the file we use to display the editor. We do that in the Index.aspx page like so:
These editor templates are stored in your controllers view folder, under a folder called “EditorTemplates” as shown below:
The OrderRow.ascx Editor will handle how each editor is formatted for editing:
We will be using another editor template, “LineItemRow” to format how each line item should be edited. We are using one editor to create another editor, cool!
Here’s the LineItemRow.ascx (which is called from above via the “LineItemRow” parameter in the EditorFor expression based call:
Incremental Sequencing
The reason the list binding works is because we’re using incremental sequencing while building our editor. We’re using for loops. This allows MVC to know where it’s at during the creation of the form. What does this mean in plain English? MVC will generate incremental name and id’s in the HTML, which, when posting back, the DefaultModelBinder will be able to bind back into the complex object type.
Lets delve into this a bit further. This screen is generated with incremental id’s, and the id’s are shown below in the screen shot for ease of understanding:
The raw HTML looks like this (click to enlarge):
Using the incremental sequencing and editor templates allows us to change the values and post them back to the form.
Example
Lets change the name of “Sneakers” to “Shoes”. Then we’ll post the form. Here’s what we see in the post (via the debugger):
Conclusion
Now you can edit the list, post the new updated results and persist them. Fairly simple.
Download the Sample Project (VS2010)
If you’re using VS2008, you can copy the files into a VS2008 MVC2 RC2 Project (minus the project) and it should work.
ASPNET MVC2: TempData Now Persists
Feb 26th
In MVC1, TempData did not persist:
The value of TempData persists only from one request to the next. (source)
This has changed in MVC2. The TempData dictionary will not persist data until the key is read from the dictionary.
…We’ve changed the implementation in MVC 2 slightly as a result: the value will be removed from TempData after the request in which it is read, so it will continue to exist in your TempData dictionary until you display it in some page. This allows a multi-redirect scenario (such as Windows Live ID login) to use TempData and have it still be hanging around until you’re ready for it. (source)
This is very important to note because if you use MVCContrib’s ModelStateToTempData attribute the values in the modelstate will be persisted across requests as it will be present in the TempData across requests. If you’re using button names as decision criteria in your Http Post then you might notice that your ModelState is traversing into other actions and showing invalid validation on pages where the model is not of the given type of the model state info that is being presented.
ASP.NET MVC TempData Extension Methods
Feb 25th
This is an sister class to some Session classes I wrote awhile back (which I never posted). I originally wrote these extension methods for the code camp eval system for Twin Cities Code Camp last year. But I needed it today at my client so I figured I’d post it here for future reference … and so you could use it.
public static class TempDataExtensions
{
public static void Put<T>(this TempDataDictionary tempData, T value) where T : class
{
tempData[typeof(T).FullName] = value;
}
public static void Put<T>(this TempDataDictionary tempData, string key, T value) where T : class
{
tempData[typeof(T).FullName + key] = value;
}
public static T Get<T>(this TempDataDictionary tempData) where T : class
{
object o;
tempData.TryGetValue(typeof(T).FullName, out o);
return o == null ? null : (T)o;
}
public static T Get<T>(this TempDataDictionary tempData, string key) where T : class
{
object o;
tempData.TryGetValue(typeof(T).FullName + key, out o);
return o == null ? null : (T)o;
}
}
The code above allows you to put values into TempData in a strongly typed fashion. You can then get the values back out (safely) without worrying about an exception being thrown (temp data will throw if the key is not found).
Usage:
var customer = new Customer();
TempData.Put(customer); // Strongly typed without key
TempData.Put("key1", customer); // Strongly typed with extra key
var tempDataCustomer = TempData.Get<Customer>(); // Get customer without key
var tempDataCustomerWithKey = TempData.Get<Customer>("key1"); // Get customer with key
My CODE Magazine Article Is Live
Feb 25th
My first article with CODE Magazine is live. You can read it here. I cover ASP.NET MVC with the Spark View Engine. I cover a wide range of things and go in depth with examples with topics that include:
- Inline Code
- Element Constructs
- Partials
- Layouts
- and more …
I hope you enjoy it! I enjoyed writing it.
A big thanks goes out to Chris G. Williams (twitter)for doing a stellar job on the tech editing.
Read it Here
Spaghetti Code Podcast: Android Development
Feb 23rd
I was recently welcomed onto the set of another Spaghetti Code Podcast with Jeff Brand a few weeks back. Jeff and I discussed Android development, mobile development and what I think Microsoft needs to do to succeed in the world the mobile platform.
Note: Part of the conversation seems to insinuate that Flash is the be-all-end-all platform for development. This is not my stance, however, I do feel that some sort of cross platform rich client development standard is going to have a major impact and possible market share once it is introduced across mobile platforms. If that becomes flash/air, so be it. Just wanted to clear that up.