About three weeks ago, I started an ASP.NET MVC project. What I’m attempting to do is build a web app that helps leaders of very small organizations (like a church small group or a scout troop) keep track of their members and projects. For someone with a grand total of 4 weeks experience in C# and no real experience with model-view-controller architecture (I don’t think RailsBridge really counts), it was… not unambitious, I suppose.
Step one: get CRUD functionality for people and groups, and then functionality to add and remove people from groups.
At least, that was what step one was supposed to be. I’m using Entity Framework (code-first) and the magic that was supposed to happen to set up my database + tables (pretty simple–Groups, People, join table for the many-to-many relationship between Groups and People) was just… not happening. The entire first week and a half of my project time was dedicated to uninstalling and reinstalling SQL Server Express and trying to figure out a) why I could create databases in SQL Server Management Studio but did not have permissions to read/write to/from them anywhere else and b) why EF was refusing to cooperate with anything even vaguely related to data management. Finally, I took my laptop to a friend who is an (ex) SQL DBA and, after futzing with it for over an hour, he finally hit upon that I was missing the SQLClient installer that allows SQL Server Express and everything else ever to talk to one another. For some reason, it hadn’t downloaded and installed… still no clue what happened there, but hooray! I finally had a working database and could do things!
Setting up CRUD actions with Entity Framework is stupid easy, so getting part one of step one ready to go went by fairly quickly. I could create, read, update and delete (just in case you missed the acronym) people and groups, but now I had to somehow convince my app to put people into groups and take them out again. Actually, to break down step one even further, I just had to get the people into groups.
Working with the MVC pattern when you only have a shaky grasp of what it actually is can be a little daunting (okay, a lot daunting). I knew that I had a
Person object, and a
Group object, and the Person object had an
ICollection<Group> called “Memberships” and the Group object had an
ICollection<Person> called “Members”. Now how did I get them to play nice? I got it working (admittedly with some help from the prior-mentioned friend) so that the primary key (
PersonId) of the Person into the view and inserted a text box so that the user could put in the primary key for the Group (
GroupId) in. This then passed back to the controller, which looked up the Person in the database and added them to the Group’s member list. So, it worked, but it wasn’t nice. No real user would remember the IDs of all their groups and use a text box, so I decided to do it with a dropdown menu. Dropdown menus are easy, right? Select a group, press a button, and off you go!
Not so much. More on the dropdown fiasco to come in Part Two…