Suppose we have a factory for returning partials containing the logic to select a certain one. I would like some to delegate that responsibility to a factory and then write a neat code inside the controller:
[HttpGet]
public PartialViewResult GetQueryItemForCategory(string categoryName, bool campaignSelected)
{
return QueryItemBuilderFactory.BuildPartial(categoryName, campaignSelected);
}
But I really cannot call the PartialView() method inside that factory.
public static class QueryItemBuilderFactory
{
private static Dictionary<string, Func<bool, PartialViewResult>> _builderActions =
new Dictionary<string, Func<bool, PartialViewResult>>();
static QueryItemBuilderFactory()
{
_builderActions.Add("Data Field", campaignSelected =>
{
return PartialView("_DataFieldQueryItemPartial");
});
}
public static PartialViewResult BuildPartial(string categoryName, bool campaignSelected)
{
return _builderActions[categoryName](campaignSelected);
}
}
Is there any way to implement it?
The protected
PartialViewmethods is defined on the baseControllerclass:So inheriting from this
Controllerclass enables you to use this method, whereas it’s not available in other situations.However, as you can see the
PartialViewmethods returnPartialViewResultobjects so replacingin your example with
will do the trick.