2

I have a MySmartAnchor method that outputs a Html link. Depending on an object that I pass to MySmartAnchor it will set specific css class and tooltip.

I feel like this is too much logic to put in a View. I don't want to add this method to HtmlHelper methods because it addresses a specific scenario. So I'm thinking of putting this logic inside of a controller and calling it from a View like so @Model.MySmartAnchor(someObject)

Am I on the right track? Should this be done in a View, put inside of a helper method, or controller? I also thought about having a partial but it seems to me it is overkill for a link.

1
  • Maybe a solution would be to scope HTML helper methods. This way I can write a html helper method that is specific to this scenario and scope it to this page. Commented Sep 7, 2011 at 16:05

1 Answer 1

4

Seems like good candidate for creating razor helper. If that's only single view specific helper, you should create that into same view that uses it. Else, create helper into App_Code, for using it across multiple views. Referenced article by ScottGu covers that as well.

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

2 Comments

I was about to suggest an HtmlHelper until I saw your answer. Did not know that Razor helpers could be used at the app level. Thanks!
Looks like scoped helper is the answer. I was confused because I thought if I create a helper method it will be available on every View. I was able to put the helper methods in its own class and scope it in its own namespace that is specific to the View I'm working with.

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.