Search code examples
asp.net-mvcmodel-view-controllercastle-monorail

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);
   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

Solution

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