I'm looking to 'generalise' some code in a .NET 3.5 MVC application and have stumbled into a problem.
Background
I have a SomeController class with some actions:
public ActionResult Renew(string qualification, int tierId) { ... }
public ActionResult Reinstate(string qualification, int tierId) { ... }
public ActionResult Withdraw(string qualification, int tierId) { ... }
In the View I use a strongly typed extension helper method to create an ActionLink (this generic method comes from Microsoft.Web.Mvc.dll
). Sample use would be
Html.ActionLink<SomeController>(c=>c.Renew(Model.Qualification, Model.TierId),"Renew")
Only that I have a property on the view that defines the expression for the method call so I can do this:
Html.ActionLink(Model.Action,"Renew")
The Action property on the View Model has the following signature
public Expression<Action<SomeController>> Action { get; set; }
When I create the view in a controller action I can use the following code:
model.Action = c => c.Renew(dto.Qualification.Name, t.Id)
Approach
So far so good, all strongly typed and working nicely but this is where my question starts. I want to be able to replace the explicit call to c => c.Renew(dto.Qualification.Name, t.Id)
with a parameter that is passed into the method that assembles the view model.
I was hoping to do the following:
private SomeViewModel CreateViewModel(SomeDto dto, Tier t, Func<string, int, ActionResult>> actionToCall) {
return new SomeViewModel {
...
Action = a => actionToCall(dto.Qualification.Name, t.Id),
}
}
and then use this method to construct the model and pass the appropriate controller action as a parameter
var model = CreateViewModel(dto,tier,this.Reinstate)
Problem
This solution compiles but when I get to rendering the ActionLink I get an expection because the expression passed in as an action is not of MethodCallExpression
type. This exception is thrown by the Html.ActionLink extension method from the View.