I’ve been working on a project with the Asp.Net MVC framework lately, and thought I’d write up some lessons learned. I’m using Professional ASP.NET MVC 1.0 as a guide. The first chapter is available online for free as a pdf.
One: Be absolutely certain you are avoiding web forms web controls. I took the master page from a demo version of the application that was in web forms, and it still had a couple of form tags and server controls. This causes the dreaded “Viewstate Mac validation” failure, but only in certain cases, like re-rendering a view after a post.
Two: RenderPartial is a great way to keep things clean and simple. Learn it right away.
Three: Do NOT name a partial and a page in the same folder the same thing. RenderPartial will look for pages and user controls, so you can get some surprising behaviour (pages before user controls). Worse yet, this got me in an infinite loop.
Four: This is sort of a linq to sql hint. Say you have a form for the parent when you already now the child. For example, you are adding a line item and you know the product. You can hook on the product, to the line item by id, but not the actual reference. That reference would be nice to allow your form to display information about the product (say product.name). This is a great problem to solve with ViewModels. Create a custom model for your view, where display information is stored in properties.
I know I’ve bashed Linq to SQL in another post on here. It has it’s issues. But for rapid development, it’s pretty handy.
Comments
One response to “Asp.Net MVC Gotchas”
For #2 … when you upgrade to MVC 2 RC, you get RenderAction which is also very helpful because you get controller logic along with a partial view. Very nice for menus.