0

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

3 Answers 3

3

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.

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

1 Comment

I believe if you add one, Scott Hanselman will come to your house and kick you a$$.
1

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.

2 Comments

can i write any kind of logic for view itself also in controller?
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).
0

@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

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

Comments

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.