2011-04-08 1 views
0

J'ai l'interface de service suivante:Méthode de la couche de service ne sont pas disponibles dans la méthode d'action du contrôleur

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

Voici mon implémentation de l'interface:

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

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

Mon contrôleur:

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

S'il vous plaît voir dans ma méthode Create, j'essaye de créer mon modèle de vue à travers le service, mais whe n Je clique. alors il ne semble pas y avoir une option pour sélectionner CreateEditGrantApplicationViewModel, seulement GetAll est là. Pourquoi est-ce? J'ai toutes les bonnes références.

Répondre

1

Vous devez définir

EditGrantApplicationViewModel CreateEditGrantApplicationViewModel(); 

sur votre interface IGrantApplicationService. À l'heure actuelle, le contrôleur ne connaît que ce qui se trouve sur l'interface et non la classe.

+0

J'injecte une instance GrantApplicationService. –

+0

Mais votre définition n'a que la méthode FindAll() - le compilateur ne sait pas quelle implémentation de cette interface sera transmise au moment de la compilation. Vous devriez invoquer la méthode par réflexion (ce qui serait un anti-pattern dans cette circonstance) –

1

Votre champ grantApplicationService est déclaré IGrantApplicationService mais votre méthode CreateEditGrantApplicationViewModel ne se trouve pas dans l'interface. Il suffit d'ajouter CreateEditGrantApplicationViewModel dans l'interface et tout ira bien.

Questions connexes