Asp.net MVC 2, MvcContrib, and a base controller with redirect actions
I've got a base controller that takes a couple generics, nothing overly fancy.
public class SystemBaseController<TForm, TFormViewModel> : Controller
where TForm : class, IForm
where TFormViewModel : class, IViewModel
ok, no big deal. I have a method "CompleteForm" that takes in the viewModel, looks kinda like this .开发者_Go百科..
public ActionResult CompleteForm(TFormViewModel viewModel)
{
//does some stuff
return this.RedirectToAction(c => c.FormInfo(viewModel));
}
Problem is, the controller that inherits this, like so
public class SalesFormController : SystemBaseController<SalesForm, SalesViewModel>
{ }
I end up getting a error from MvcContrib - Controller name must end in 'Controller' at this point ...
public RedirectToRouteResult(Expression<Action<T>> expression)
: this(expression, expr => Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression(expr)) {}
The expression that's passed in is correct (SystemBaseController blahblah) but I'm not sure why its 1.) saying there's no controller at the end, and 2.) if I pull out everything into the controller (out of the base), works just fine. Do I need to write or setup some kind of action filter of my own or what am I missing?
OK, now that I see all this written out I think I see the problem.
MvcContrib figures out which controller to call by inferring from the lambda expression that you passed in, not the controller type. So when you say this.RedirectToAction(c => c.FormInfo(viewModel));
, it looks at the lambda expression and infers that T is of type SystemBaseController<TForm, TFormViewModel>
, not SalesFormController.
What you might have to do is change your base class to SystemBaseController<TForm, TFormViewModel, TController>
so that you can say this.RedirectToAction<TController>(c => c.FormInfo(viewModel));
. That might work.
精彩评论