MVC Form Helper

by Seth Juarez 23. July 2008 11:50

Moving away from ASP 3.0 Request.Form(" ...

As mentioned in my previous post, I wanted to find a way to auto-populate and entity object from the Request.Form collection. I also wanted (as an aside) to see if the grid still worked on MVC Preview 4 (it does). Here is the code:

public static T Request<T>(this Controller controller) where T : class, new()
{
    T o = new T();
    var request = controller.Request;
    foreach (var property in typeof(T).GetProperties())
    {
        // Check if the object has the appropriate property
        var q = (request.Form.AllKeys
            .Where<string>(s => s.ToLower() == property.Name.ToLower()))
            .ToList<string>();

        // if more than one... ignore
        if(q.Count == 1)
        {
             string datum = request.Form[q[0]];
             if (property.PropertyType == typeof(string))
                 property.SetValue(o, datum, null);
             else
                 property.SetValue(o,
                    Convert.ChangeType(datum, property.PropertyType),
                    null);
         }
    }

    return o;
}

Notice that this is not an extension on the HtmlHelper since this will only be used inside the controller (hence a controller extension method). Here is how it is used:

Student s = this.Request<Student>();

Now for the explanation! The constraints on T (line 1, the where part) say that T must be a reference type and have a parameter-less constructor. I guess it doesn't have to have a parameter-less constructor but I thought it would be a pain to find out which request key matched up with which parameter in the constructor. I also thought that Entity classes should be POCO objects so they would naturally have an empty constructor. Some screenshots:

Before:

image

After:

image

The Grid

I am continuing to work on the grid (this was a part of it). I just haven't gotten around to implementing the next thing all the way. The goal again was to have more control over how the grid should be rendered. I am borrowing some of the ideas from the MVCContrib grid but simplifying it a whole lot (for simple users like myself)

MVC Preview 4

I uninstalled preview 3 and then installed preview 4 and shortly after found that VS could not open my preview 3 stuff. Instead of rigging it, I just re-created the MVC part. The Controls assembly (the one with the grid) worked perfectly after correcting the references to the MVC dll's.

Tags:

ASP.NET | MVC

Comments

8/11/2008 7:01:46 AM #

Nice one. Next step is to implement form validation ;)

Bjarte Djuvik Næss Norway |

1/3/2009 3:16:17 AM #

When's part 4 coming? I think this may be the best MVC Grid for my purposes that I've found so far, but it's not quite finished, huh?

Mufasa United States |

8/18/2009 9:14:26 AM #

That is a good question! I think I have opted for showing general principles in designing custom controls. I might later pick up the grid idea (even though mvccontrib has a great one) and complete it.

Seth United States |

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

Pages

RecentComments

Comment RSS