views:

83

answers:

3

Hello all,

I have an asp.net mvc project, its build just from views, controllers, models, and other files. I have any codebehind file in my views. Does it makes sense to create them for using sometime? Is it some situation when they giving more abilities and advantage for developer?

Thanks and take care, Ragims

+1  A: 

The codebehind files are there for compatibility and for the rare case where you need logic in the View itself. Try very hard not to have any code in the codebehind, as it breaks with the very idea behind MVC to have code directly bound to a View.

Dave Swersky
can i write any kind of logic for view itself also in controller?
Ragims
It's important to understand that views should only use the data provided to them by controller GET actions via ViewData or via Model and operate on that. If you require any other logic it should be implemented as AJAX calls (via your favorite AJAX library, for instance JQuery) or by using postbacks (posting back to controller actions marked with POST).
mare
+3  A: 

No, they make absolutely no sense in ASP.NET MVC views. By the way they are completely removed in ASP.NET MVC 2.0 views. You could make aberrations by adding one but you shouldn't.

Darin Dimitrov
I believe if you add one, Scott Hanselman will come to your house and kick you a$$.
Martin
A: 

@Ragims,

No matter whether Scott Hanselman might come come and kick my a$$ or not, I embed any and all code that I need in my Views, inside <% %> tags.

That being said, I try to get most processing done in a model, triggered from the controller code.

I don't think you should use codebehind, but embed what you need in the page itself. Keep it as clean as you can, and use server side comments inside <% // this is c# comment %>

when you can't keep it clean.

You will be better served doing stuff in a model or controller in the long run.

Codebehind vs CodeInPlace conveys no advantages that I know of.

CodeInPlace at least lets you see the code in the context of the html.

Yeah, Yeah, I know it reminds us all of the asp days. Some things in those days were pretty clear. ymmv

Bob Housedorf