使用操作筛选器修改操作内部的值



所以我读了不同的堆栈Q/A,但我仍然很困惑。。。

所以,当一个请求被发送到控制器时,它会进行一些处理,并将一些结果发送回视图。现在,就在它将数据发送到视图之前,我需要查看它的数据并进行一些更改,然后简单地允许操作正常继续。

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

[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);
}

我需要修改操作过滤器中每个博客文章的CreatedOn值:

public 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)
    {
    }
}

如果您可以更改操作或业务逻辑,最好将逻辑置于操作或您的业务逻辑方法中。但如果您不能更改或覆盖控制器方法,则可以创建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);
    }
}

最新更新