Was doing some reading today about Razor Syntax with MVC Framework and was wondering why would/should I use Razor? What benefit does it provide over doing the same thing in the code behind and/or controller?

As an example I saw, they showed how you can use Razor to display the current time, doing arrays and loops, and other functions using inline coding right there within the HTML. Wouldn't this provide excess clutter to the front end that can easily be provided with the code behind?


More generally, you are asking about the benefits of using a template rather than generating content directly inside your source code.

The answer is that mixing source code and HTML could be a viable alternative for small applications, but doesn't scale well. Once your project becomes large enough, it becomes too difficult to maintain code where chunks of HTML are all over the source code.

Using a template solves this issue. All your logic resides in controllers, while HTML is put in templates, which, in MVC, are called views. When someone wants to modify HTML, there is no need to deal with the business logic: only the view is affected. It might also work the other way around: you can swap the logic while letting HTML unchanged (as soon as the models remain the same).

So if templates are for HTML, what's all this source code in Razor?!

Indeed, you still need some logical statements within your templates, usually in a form of conditions or loops. If you need to display an avatar of a person only when the person actually has an avatar, well, that's a good place for an if in your template. If you need to display every product which was provided by the controller through the model, there would be a foreach.

The fact that you can write any code within your views doesn't mean you should. Be careful to keep business logic inside controllers. A few conditions and loops is fine, but if the code in your views becomes too complicate, it's a good sign that you've done too much. So:

  • All business logic should be in controllers (or business classes called by the controllers, depending on the N-tier architecture you use and the complexity of your application).

  • The models contain simple objects (usually POCO) which can be used easily, without too much code.

  • The views contain only the most elementary logic needed exclusively to generate HTML from the model. No complex business logic here.

  • Thanks for taking the time to reply and I see what you mean. I understand that business logic needs to be maintained in controllers / business classes and that models should store all your objects that you use to populate with data. But With all that though, why should i go and do say @Datetime.Now to get the time when i could create a label and say timeLabelValue.text = datetime.now in the C# code behind. To me that feels cleaner and maintains organization to allow the HTML to do it's thing and the C# to do it's thing. – ggiaquin16 Mar 17 '16 at 22:58
  • @DateTime.Now is just an illustration good for tutorials, but not much else. This is exactly the stuff which should go in controllers and passed to the views through the models. What do you mean by timeLabelValue.Text example? You are talking about ASP.NET, right? – Arseni Mourzenko Mar 17 '16 at 23:01
  • Yes, what i mean is having in the HTML ASP:Label ID="timeLabelValue" and then in the codebehind you can call that label and say timeLabelValue.Text = DateTime.Now(); – ggiaquin16 Mar 17 '16 at 23:06
  • 1
    ASP.NET has a very different model compared to ASP.NET MVC. In ASP.NET, you have HTML in a sort of a template (which is not), and the codebehind. What happens is that business logic is scattered around, and it's very easy to end up with SQL queries in templates or with chunks of HTML in codebehind. ASP.NET MVC makes it much less natural to do this; this is a good thing, but it also means that in order to set a value of a field to something which comes from your business logic, you have to change the controller, the model and the view all together. – Arseni Mourzenko Mar 17 '16 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.