我创建可以由用户(仅在某些页面)来设置自定义颜色的站点设置ViewBag。我想在ActionFilterAttribute中获取这些数据并将其设置在ViewBag中,以便我可以在我的_Layout.cshtml中获取数据。从ActionFilterAttribute
这是我ActionFilterAttribute ...
public class PopulateColorOptionsAttribute : ActionFilterAttribute
{
private readonly OptionsDataHelper optionsDataHelper;
public PopulateOptionsAttribute(OptionsDataHelper optionsDataHelper)
{
this.optionsDataHelper = optionsDataHelper;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await base.OnActionExecutionAsync(context, next);
// Get the cemetery data and set it on the view bag.
var personId = Convert.ToInt32(context.RouteData.Values["personId"]);
context.Controller.ViewBag.OptionsData = await optionsDataHelper.GetValueAsync(personId, CancellationToken.None);
}
}
不幸的是,我收到ViewBag,指出错误:
'对象' 不包含一个定义为 'ViewBag' 和没有扩展名方法'ViewBag'接受类型'对象'的第一个参数可以找到(你是否缺少使用指令或程序集引用?)[dnx451]
我很确定我对滤波器没有正确理解,并希望能够指导我如何实现我期望的目标。
我只是好奇@JasCav,做我的回答帮助? –