MVC Logic, Views and Helpers - A Specific Story

Me and a colleague are discussing how we would generate a link on a page. Should we use html helpers or keep very simple view logic in the view?

For this project we are using Castle Monorail and the NVelocity view engine. I would be grateful to anyone who considers both options below and gives their opinions.

In this story, the link is currently only used on a single page.

Option 1 - With Helper

Helper Code

 var action = snail.IsActive ? "ConfirmDeactivate" : "ConfirmActivate";
   var routeValues = new Dictionary<string, string>
                       {"action", action},
                       {"querystring", "id=" + snail.ID}
   var href = UrlHelper.For(routeValues);

   var link = new XElement("a");
   link.SetAttributeValue("href", href);

   return link.ToString();

And then in the view, we just call the helper like so:


Option 2 - All in the View

  <a href="$Url.For("%{action='ConfirmDeactivate', querystring='id=$snail.ID'}")">Deactivate</a>
   <a href="$Url.For("%{action='ConfirmActivate', querystring='id=$snail.ID'}")">Activate</a>


For me I prefer option 1. It is more convenient to do the logic on the helper and more elegant.

I will strogly suggest/use option 1 - irrespective of number of views you are developing

  • Make your view Dumb. - doesn matter for a a single view or 100 views.

Lots of support for Option 1 then - time to play devil's advocate!

One of the main reasons for applying concepts like DRY is to make the application easier to change/more maintainable. So:

  • What if the text in the link needs to change later? Or I want to put an image in there instead of the text? Do I really want to have to rebuild the application to remove a typo in a link?

  • Worse than that, what if the link text needs to be different on different pages? Do I then add another helper method for each page?

  • That's assuming it's me that's making the change. What if I ask my markup guru to change the link to an image, or add a class to the a element to style it up? He's perfectly happy to edit view files, but if he saw the helper call he'd have no idea where the HTML was coming from and where to change it. Even if he found the helper class would he know how to change the C# code to do what he needs? All he's trying to do is change a bit of text! Why does it have to be so difficult?

  • I could alleviate some of the above issues by adding two method parameters on the helper, and passing in the text for each link, but what if the text was an image tag? I've now got HTML that I need to escape in my helper call - that's going to look pretty messy. I still couldn't edit the link element's attributes either, should I add a parameter/parameters for that as well? Where does it stop?

In essence I'll argue that I can show Option 2 to most people and they'll understand it and be able to modify it. Nice plain HTML with some easy to follow conditional logic. Option 1 adds complexity and abstration that I don't need.

Need Your Help

What exactly does a synchronized method do?

java methods synchronized runnable

I'm creating a game using Swing. I made start() and stop() synchronized because I was told it was better. What does synchronized do and what is the advantage in using it?

Convert Shortcut Hotkey combination to real keys?

.net winapi hotkeys shortcut-key

How I could convert the Hotkey property of a shortcut to represent the combination in String type or directly convert the value to the right combination?