PAGES

27

Apr 08

Digging into ASP.NET MVC – Part 2



I got a bit sidetracked from the MVC stuff as I didn’t have as much time between projects as expected, but had a chance today to play around with it a bit more at home. I don’t really have any earth shattering things to share, but I did run into a couple of beginner gotchas here and there. Still not quite wrapping my head around certain things.

I had set up my home machine with Visual Studio 2008, but I guess I never finished setting things up. I had copied my MVC projects over and was having trouble getting certain things to work properly until I realized I hadn’t installed the MVC bits yet. Doh. So that helped. I had a couple of the dlls in the bin directory, so a lot of things actually worked, but the Visual Studio tie-ins weren’t there. Also, you really can’t run an MVC project by opening from the file system, you do have to use the MVC project type in order to get everything wired up correctly. That might be obvious, but if you have gotten used to just pointing at a directory to quickly open a web site to play around with, that won’t work.

On my new project we are using LINQ extensively, so when I last played around with MVC I was also trying to figure out LINQ and between the two had a bit of a learning curve. I’m starting to get more comfortable with LINQ, so that helped me get up and running a lot more quickly this time. I figured out how to use the LINQ to SQL objects as a ViewData source for my MVC views,so that is kinda cool. If you want to just spit our html using a for each type pattern,you can even cast the objects back into your LINQ to SQL object and get strong-typing in your view. Not sure if that is kosher or not, but hey I’m still learning. Guthrie does it a bit, so I think I might be safe. You can also use the standard ASP.NET data controls like grid views, repeaters, etc., but that might get you into trouble. More on that later.

My personal web site has a lot of simple pages where I just get and display some data, such as favorite movies, music and so forth. MVC works great for that. In fact, I was able to rewire about 6 pages in an afternoon, including LINQ data access. So that is pretty sweet. Also really like the Html.ActionLink syntax for links, as it gives you a lot of security against moving stuff around. As long as your controllers know where to find the views, the actual view can change as much as you want in location, name, etc. I’m still looking for a good way to use an image rather than text though. People have a few ideas, but this feels like an area that needs improvement.

After getting data display working on a number of pages, the next thing I need to investigate is saving data back to the database. There are a couple of things that are a bit of a paradigm shift here. One, you don’t have server controls, postback, viewstate, etc. So in some ways it is a step back to some older ways of doing things, but it gets you back to a very clean html entry form again, but with all the power of the latest .NET stack working for you. Two (and this is the trouble I was talking about), you can’t have a server form on the page or stuff just doesn’t work. I was trying to follow some tutorials and couldn’t figure out what I was missing. Typically you create a standard html form and set it to post to an action your controller understands. Mine was just posting back to the same view and I couldn’t figure out why until I realized I also had another standard asp.net form (runat = server) on the page that was confusing things. Once I removed that, I started getting the behavior I expected. Unfortunately, I was at a point where I needed to stop so I’ll have to continue that learning curve on another day. Seems reasonably straight-forward now that I’m starting to wrap my head around it though.

Comments Off , permalink


Comments are closed.