使用操作过滤器修改操作内的值

时间:2015-11-27 04:33:34

标签: c# asp.net-mvc asp.net-mvc-5

所以我读了不同的堆栈Q / As,但我还是很困惑......

因此,当向控制器发送请求时,它会执行一些处理并将一些结果发送回视图。现在,就在它将数据发送到视图之前,我需要查看其数据并进行一些更改,然后只需允许操作正常继续。

我想修改数据的两个方法示例:

[NonAction]
protected virtual void PrepareBlogPostModel(BlogPostModel model, BlogPost blogPost, bool prepareComments)
{
    model.Id = blogPost.Id;
    model.MetaTitle = blogPost.MetaTitle;
    model.MetaDescription = blogPost.MetaDescription;
    model.MetaKeywords = blogPost.MetaKeywords;
    model.SeName = blogPost.GetSeName(blogPost.LanguageId, ensureTwoPublishedLanguages: false);
    model.Title = blogPost.Title;
    model.Body = blogPost.Body;
    model.BodyOverview = blogPost.BodyOverview;
    model.AllowComments = blogPost.AllowComments;
    model.CreatedOn = _dateTimeHelper.ConvertToUserTime(blogPost.CreatedOnUtc, DateTimeKind.Utc);
    model.Tags = blogPost.ParseTags().ToList();
    model.NumberOfComments = blogPost.CommentCount;
    model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnBlogCommentPage;
}

[NonAction]
protected virtual BlogPostListModel PrepareBlogPostListModel(BlogPagingFilteringModel command)
{
    var model = new BlogPostListModel();
    model.PagingFilteringContext.Tag = command.Tag;
    model.PagingFilteringContext.Month = command.Month;
    model.WorkingLanguageId = _workContext.WorkingLanguage.Id;

    DateTime? dateFrom = command.GetFromMonth();
    DateTime? dateTo = command.GetToMonth();

    IPagedList<BlogPost> blogPosts;

    if (String.IsNullOrEmpty(command.Tag))
    {
        blogPosts = _blogService.GetAllBlogPosts(_storeContext.CurrentStore.Id,
        _workContext.WorkingLanguage.Id,
        dateFrom, dateTo, command.PageNumber - 1, command.PageSize);
    }
    else
    {
        blogPosts = _blogService.GetAllBlogPostsByTag(_storeContext.CurrentStore.Id, 
        _workContext.WorkingLanguage.Id,
        command.Tag, command.PageNumber - 1, command.PageSize);
    }

    model.PagingFilteringContext.LoadPagedList(blogPosts);
    model.BlogPosts = blogPosts.Select(x =>
                      {
                          var blogPostModel = new BlogPostModel();
                          PrepareBlogPostModel(blogPostModel, x, false);
                          return blogPostModel;
                      }).ToList();

    return model;
}

然后在这里调用其中一个:

public ActionResult List(BlogPagingFilteringModel command)
{
    if (!_blogSettings.Enabled)
        return RedirectToRoute("HomePage");

    var model = PrepareBlogPostListModel(command);
    return View("List", model);
}

我需要修改动作过滤器中每篇博文的CreatedOnpublic class ChangeDateActionFilter : ActionFilterAttribute, IFilterProvider { public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) { // making sure we are modifying the right controller and action data if (controllerContext.Controller is BlogController && actionDescriptor.ActionName.Equals("PrepareBlogPostListModel", StringComparison.InvariantCultureIgnoreCase)) { return new List<Filter>() { new Filter(this, FilterScope.Action, 0) }; } return new List<Filter>(); } public override void OnActionExecuted(ActionExecutedContext filterContext) { // modifying CreatedOn for each blog post here, but how? } public override void OnActionExecuting(ActionExecutingContext filterContext) { } } 值:

dnscmd /enumzones /Forward | out-file C:\temp\zones.txt

1 个答案:

答案 0 :(得分:1)

如果您可以更改操作或业务逻辑,最好将逻辑放入操作或业务逻辑方法中,但如果您无法更改或覆盖控制器方法,则可以创建ActionFilter并覆盖OnActionExecuted并使用filterContext.Controller.ViewData.Model修改模型:

public class SomeFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var model = filterContext.Controller.ViewData.Model as YourModelType;

        //Modify model here or assign a new object to it.

        //Then pass the modified model to view
        filterContext.Controller.ViewData.Model= model;

        base.OnActionExecuted(filterContext);
    }
}