GurdeepS
GurdeepS

Reputation: 67273

ASP.NET MVC3 first time questions

I am building my first mvc3 app. A few questions I have are:

1) the razor view engine lets me embed code into the views. Is this not what we were once trying to get away? ie keep code out of the aspx.

2) Do models need to implement an interface?

3) Do models need to have methods? Or just properties?

Thanks

Upvotes: 1

Views: 214

Answers (4)

IAmGroot
IAmGroot

Reputation: 13865

1) the razor view engine lets me embed code into the views. Is this not what we were once trying to get away? ie keep code out of the aspx.

No, we were once trying to get the logic out of the view. This gives a bit more control over the view, but should not be used as a method of implementing logic.

2) Do models need to implement an interface?

Nope.

3) Do models need to have methods? Or just properties?

Models are just classes. They define the structure of your class.

Upvotes: 1

Rob Levine
Rob Levine

Reputation: 41338

1) It may seem a bit like that, but really it depends what the code is. IMHO You should really avoid any logic or code in the view, other than that directly related to rendering the view. For this code though, Razor gives a lovely clean way of coding in the view.

2) No - any class can be a model.

3) There is nothing to stop you putting methods on the model - but really they should be very simple data tranfer objects - they just "carry" data around. So more often than not, stick to properties.

Upvotes: 1

Anders Marzi Tornblad
Anders Marzi Tornblad

Reputation: 19335

  1. The code that you put in the view is supposed to be rendering code only. Simple for loops for repetition, calls to EditorFor or DisplayFor or stuff like using (Html.BeginForm()). The main business logic should never be placed in the View layer.
  2. No.
  3. No, just properties. You can add really simple helper methods, but the important stuff is the properties, so even the helper stuff should be implemented as readonly properties.

Actually, the first part is true for the aspx engine and WebForms as well. And Php, and classic ASP, and...

Upvotes: 1

Yngve B-Nilsen
Yngve B-Nilsen

Reputation: 9676

Pretty vague question, but I'll give you my 5c worth:

  1. True, but the code we put in the Razor views are usually only to generate Html-controls.. the helper methods in MVC3 utilizes data attributes from your Viewmodels and generates validation etc. When that is said, it's completely optional how much code you wish to put in your views.

  2. No.

  3. Viewmodels should be as stupid (POCO) as possible, and business logic method should be put on your domain models, as the good DDD developer you are ;)

Upvotes: 4

Related Questions