Question

I have been developing MVC 3 applications using Razor. I was wondering how much code would be acceptable in my views. There were situation where I needed to create, instantiate and use an object which wasn't included in the model and using ViewModel would make the model object bloated. Now when I look at some of my views, I find them full of server side codes. What would be the best practice when dealing with such issues?

Was it helpful?

Solution

I was wondering how much code would be acceptable in my views.

If by code you mean C# code then there should be exactly 0 to be precise. On the other hand it is perfectly fine to write HTML markup in your views and call HTML helpers.

There were situation where I needed to create, instantiate and use an object which wasn't included in the model and using ViewModel would make the model object bloated

Then it seems that your view model was not adapted to this view (since this view requires additional information). So change this situation by adapting the view model to it and including everything that it needs.

Now when I look at some of my views, I find them full of server side codes. What would be the best practice when dealing with such issues?

Yes, that's horrible. Simply adapt your view models and refactor this code into your view models or controllers or even write custom HTML helpers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top