0

I have used asp.net web forms for the last four years. I am trying to familiarise myself with mvc4. Do you put sql code in the model if you are not using an orm like entity framework. For example, if you have a domain object (model class) called person then would you have functions like: person.select, person.update, person.delete.

I am confused because i thought that mvc4 was for the user interface so it doesn't seem right putting sql statements in the model.

I have read plenty of questions on here about mvc4 today but i have not found a specific answer

3
  • Your Model is just POCO classes, all of the leg work is done by the controller. The Controller populates the Model with data for use in the View. Commented Dec 16, 2013 at 16:09
  • My opinion is that the best place to write sql code is in a stored procedure and that your controller simply calls these sp's. Commented Dec 16, 2013 at 16:13
  • @DanBracuk Yes and No. If you aren't using an ORM then definitely use stored procs. It probably isn't great practice having your Controller calling the stored procs directly though. Some kind of data layer or Repository should really be used Commented Dec 16, 2013 at 16:15

1 Answer 1

1

Any CRUD functions are executed from the controller level. You may have a Data Access Layer or Repository abstracting those away but the calls to those functions will be Controller level.

If data needs passed to the view, that data is put into a view model. The view model is then passed to the view to be consumed. The view can only access the data in that model. When you want to update something, you make a call back to the Controllers Update action, the Controller can then handle it from there.

The Model itself is just a POCO (Plain Old CLR Object) which will not have any methods. The @Htmland @Ajax helpers are useful for quickly accessing the Controller Actions. The Ajax ones can return partial views which will assist with updating data on screen without post backs.

There is a pretty good MVC4 (free) video tutorial, split up into small segments so you can easily find the bit you need. Take a look at it here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks +1. Is it fair to say that the model is a domain object/data transfer object?
There will most likely be 2 types of Model. Domain Models returned from your business logic and View Models which really only get used between the Controller and View. You could pass a Domain Model to a View but if your needs were more complex than a single Entity or Result Set, you would need to pass all the required data into a View Model and then send that on to the View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.