views:

95

answers:

2

Hey :)

I am quite new to ASP.NET MVC and MVC in general. Moving from the old school "spaghetti" design practices to WebForms was a big change, but this seems bigger (but better) in my mind.

I have some questions regarding data specific tasks.

  1. For the sake of simplicity, say I have a database table called PIN with the column structure of PinId INT, Value VARCHAR(50) and another table called Entry with the columns EntryId INT, PinId INT.
  2. I have an MVC view that takes in a string value as a PIN code.
  3. The goal is to insert a row into the Entry table. To do this, I must lookup the corresponding PinId to the provided Value.

My question would be, where do I perform this lookup of data. I can easily do it in the controller where I am performing the insert of a new Entry object but is that right? Should this lookup be performed in the Model somehow?

In addition, I am following the instructions and practices outlined in WROX's Professional ASP.NET MVC 1.0. I have created a DataRepository class which handle all of my DB heavy lifting, and am utilizing the validation class as described in that book.

Any insights would be welcomed as I am a true newbie to MVC.

Cheers and thanks stackers!

+1  A: 

Take a look at ModelBinders. This allows you to define how bind inputs from the data you are sending to your action to your Model objects. Your Action can also just take an argument of the type you want to save, and in your ModelBinder, you can do some lookup against the repository, etc.

There are many good blog posts on this if you do a search, now that you know to look at ModelBinder. Scott Hanselman has a good basic one:

http://www.hanselman.com/blog/IPrincipalUserModelBinderInASPNETMVCForEasierTesting.aspx

Tony Heupel
A: 

Never place search, add, update, delete code within a controller. Actually, there should be next to no code in the controller.

In the MVC world the controller is simply a means of getting a model from the view to your business layer and vice-versa. The aim is to have as little code here as is possible.

If you can use Linq2SQl then this would be a great place to put the entities. You can then use your data repository to do not only the heavy lifting, as you put it, but also all the other little things.

Linq2SQL will create a partial class. So, you could create another partial class which will do the CRUD and search work for you.

griegs