1

I have a simple Asp.net MVC helper method in App_Code.

@helper ApprovedStatus(TimeWorkedEntry entry)
{
   if (!entry.Authorised)
   {
    var approvers =   <<get list of approvers from service here >>

    <a tabindex="0"
       role="button"
       class="exclamation"
       data-html="true"
       data-toggle="popover"
       data-trigger="focus"
       title="<b>Not yet approved</b>"
       data-content="@approvers">
        !
    </a>
}
}

My services are normally injected into the controllers within my application by Simple Injector.

What i cannot workout is how to get an instance of my services injected into a helper method. What is it I am missing?

1
  • You shouldn't. You should be passing the values to your view by adding the values to your view model within your controller. Commented Feb 3, 2017 at 14:43

1 Answer 1

1

You're missing nothing. Views should be dumb and should especially not have any logic or call any services. That's something that should be in the controller.

So in your case, the controller should pass the list of approvers on to the view through the view model. The view can pass this information on to the helper method.

This keeps the code clean, simple and testable.

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

4 Comments

Doh! of course, not sure why I thought I needed to inject it there, think I forgot it wa sreally just a bit of the view! thanks
ASP.NET Core has an @inject directive but that's just down right dirty!!! (Except for view localization)
@Luke: I would even go as far as saying that the inject directive is even dirty when doing view localization.
I'd tend to agree with you there @Steven

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.