Question

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);
   link.SetValue(action.Substring(7));

   return link.ToString();

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

<li>$Html.SnailActivationSwitchLink($item)</li>

Option 2 - All in the View

#if($snail.IsActive)
  <a href="$Url.For("%{action='ConfirmDeactivate', querystring='id=$snail.ID'}")">Deactivate</a>
  #else
   <a href="$Url.For("%{action='ConfirmActivate', querystring='id=$snail.ID'}")">Activate</a>
  #end
Was it helpful?

Solution

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

OTHER TIPS

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.

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