Tuesday, February 8, 2011

MVC and TDC: Starting Out

I just started a new project and chose the standard MVC3 template. I left the account stuff in there for now, and also the Home stuff too.

I then added an ActionResult method called SystemStatus. I altered the Shared Layout view (deleted some stuff, added in SystemStatus). I played with the ViewBad.Message and some other properties to make sure my changes were taking effect.

I added a SystemStatus View by right clicking on the method in my controller and choosing "Add View". I changed the name but left the defaults.

I then added a Tools Controller and a couple of views for that one, too. Altered the Shared Layout View to give an option for my Tools stuff. All is working.

As for unit tests, I just kind of copied the existing unit tests that were built (I said "Yes" to the prompt asking if I wanted an associated Tests project.) So, my code is covered with tests. Kind of.

Now, I need to get the Model hooked up to an existing database. I'm using TDC as the backend, and just bolting on a quick reporting/small maintenance type app to it.

So, after downloading Castle, NHibernate, Entity Framework 4, etc, I settled on EF4 for the time being.

To get this up and going, I right clicked my Model folder and chose Add New Item, ADO.NET Entity Data Model. From there, I had to feel around to connect the DB, then to choose the tables I wanted. In the future, to edit this, you have to choose to open the .edmx file, not the file under it.

It then generated a bunch of stuff (hidden) that allowed me to find objects like tbl_MarketingCode (not my name).

From the controller, I generate a list of those, and pass that as the first argument to View(). I had to goto the view itself and add in, at the top, a @model List line. Can we only pass 1 thing to a view? To get to this variable that was passed, you just reference it in the view with the Model keyword. What if it needs other things?