0

I have the following service interface:

public interface IGrantApplicationService
{
   IEnumerable<T> GetAll();
}

Here is my implementation of the interface:

public class GrantApplicationService : IGrantApplicationService
{
   public IEnumerable<GrantApplication> GetAll()
   {
      // Code here
   }

   public EditGrantApplicationViewModel CreateEditGrantApplicationViewModel()
   {
      // Code here
   }
}

My controller:

public class GrantApplicationController : Controller
{
   private IGrantApplicationService grantApplicationService;

   public GrantApplicationController(IGrantApplicationService grantApplicationService)
   {
      this.grantApplicationService = grantApplicationService;
   }

   public ActionResult Create()
   {
      // I am trying to create my view model like this and populate it with data
      EditGrantApplicationViewModel viewModel = grantApplicationService.CreateEditGrantApplicationViewModel();

      return View(viewModel);
   }
}

Please see in my Create method, I am trying to create my view model through the service, but when I click . then there doesn't seem to be an option to select CreateEditGrantApplicationViewModel, only GetAll is there. Why is this? I have all the correct references.

2 Answers 2

1

You need to define

EditGrantApplicationViewModel CreateEditGrantApplicationViewModel();

on your IGrantApplicationService interface.

At the moment the controller only knows about what is on the interface not the class.

2
  • I do inject a GrantApplicationService instance. Apr 8, 2011 at 10:21
  • But your definition only has the FindAll() method - the compiler doesn't know what implementation of that interface will be passed in at compile-time. You'd have to invoke the method via reflection (which would be an anti-pattern in this circumstance) Apr 8, 2011 at 10:27
1

Your grantApplicationService field is declared as IGrantApplicationService but your method CreateEditGrantApplicationViewModel is not in the interface. Just add CreateEditGrantApplicationViewModel in the interface and you'll be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.