Dynamic Pitfalls

November 28, 2010

I’m working on an asp.net mvc 3 powered site (for my wedding actually!), where I decided to try out some new technologies I’ve been wanting to play with for a while.

The first technology is the combination of EF 4.0 (AKA Entity Framework), and the new SQL Server Compact 4.0, which is working nice so far, and hopefully I’ll elaborate more in the matter in a future post.

The most important thing I wanted to test though was asp.net mvc 3 (RC), running on .NET 4, especially the usages of the dynamic keyword.

You can now pass dynamic values instead of the ViewData magic string dictionary, setting values on the page level, view level etc.

This introduces a blessed flexibility, and saves time.

Here’s a small example where I used it:

public ActionResult Login(string ReturnUrl)  
        ViewModel.ReturnUrl = ReturnUrl;  
        ViewModel.LoginFailed = false;  
        return View();  

I have a method for Login on my controller, which get’s the ReturnUrl from the authentication filter (AKA, if you visit a route that has the [Authorize] filter before logging in, you’ll be redirected to the Login method, and the .Net framework appends the query string ReturnUrl to the request).

Peaches, now for the view:

@using (Html.BeginForm())  
    <input type="hidden" name="ReturnUrl" value="@View.ReturnURl" />  
        @Html.TextBox("id") Password  
    <button type="submit">Login</button>  
if (View.LoginFailed)  
    <div class="error">Wrong Password!</div>  

Does of you with a sharp eye have already seen what I failed in.

<input type="hidden" name="ReturnUrl" value="@View.ReturnURl" />  

Should have been:

<input type="hidden" name="ReturnUrl" value="@View.ReturnUrl" />  

And yes, a unit test would have caught that.


comments powered by Disqus