Use named objects instead of strings in your ASP.NET MVC apps

One of the frustrating things about ASP.NET MVC is the use of strings to represent objects that should be able to be referenced directly.  This way, the compiler could detect your typos and intellisense could offer valid, existing object names.

While this functionality doesn’t come out-of-the-box with Visual Studio, you can add it with T4MVC.

Here’s an example of what you do without T4MVC:

@Html.ActionLink("Delete User", "Delete", "Users", new { id = Model.UserID }, null)

Here’s how you do it with T4MVC:

@Html.ActionLink("Delete User", MVC.Users.Delete(Model.UserID))

Pretty much everywhere you’d use a string to reference a type, an object, a controller, or an action method, you can now use with actual, named types and objects.  The compiler, rather than your users, will detect your errors and intellisense will provide for you available items while writing code.

Leave a Reply

Your email address will not be published. Required fields are marked *