Jonas Stawski
Jonas Stawski

Reputation: 6752

ASP.NET MVC: Am I doing it right?

So I am an old web forms guy and new to the MVC (in general, not only ASP.NET) framework. My views are starting to look a lot like the good old classic ASP. Not that i'm adding any business logic or anything, but more of a presentation logic. I end up with a lot of <% %> tags and if/else statements for deciding links to display or styles to use.

I also thought of deciding the styles or links within the controller and setting them on the model, but sounds like breaking the MVC purpose.

I end up ignoring the <% %> to make sure my HTML is well formed.

I want to hear your opinion. Are your views the same as mine? Am I doing something wrong?

Upvotes: 2

Views: 140

Answers (4)

Kevin Pang
Kevin Pang

Reputation: 41442

Yes, you're doing it right. ASP.NET MVC isn't an improvement over classic web forms in every respect. It has its advantages and disadvantages ("tag soup", as you've discovered, being one of the disadvantages).

There are a few ways to alleviate the pain (moving as much logic into the model, HTML helpers, partial views, etc.), but it's difficult to avoid.

Upvotes: 0

Craig MacGregor
Craig MacGregor

Reputation: 4629

I usually create a ViewModel class specific for each view that contains any logic associated with the specific view. This is good for situations where the outcome of a condition is just a simple DIV or SPAN tag which don't really warrant their own extension or partial view.

I find it will clean up a lot of the classic ASP'ish look in my views.

See Stephen Walther's blog for more info on this approach.

Upvotes: 0

Bryan S.
Bryan S.

Reputation: 1444

Along with what mxmissle said (I voted him up) says I will do a partial view to move complex areas of a page to a seperate file, it helps clean things up as well as code reuse.

I find if things are looking a bit too old school ASP it's time to refactor. It's surprising what you can clean up into a helper class or partial view, or simply redo using something more concise.

Edit: Also, if it's looking a bit too old school ASP, perhaps you have logic in your view that doesn't belong there.

Upvotes: 1

mxmissile
mxmissile

Reputation: 11673

If I have a ton of presentation logic, I try to move that to an extension off the HtmlHelper class.

Upvotes: 2

Related Questions