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

瓦希德·阿米里(Vahid Amiri)

所以我对此阅读了不同的堆栈问答,但我仍然很困惑...

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

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

[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)
    {
    }
}
雷扎·阿盖伊(Reza Aghaei)

如果可以更改操作或业务逻辑,最好将逻辑或操作或业务逻辑方法付诸实践,但是,如果您不能更改或覆盖控制器方法,则可以使用创建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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用动作过滤器更改输入参数

来自分类Dev

将服务注入动作过滤器

来自分类Dev

生成由动作过滤器控制的PDF?

来自分类Dev

阻止在动作过滤器中执行

来自分类Dev

覆盖的动作是否从基本动作继承动作过滤器?

来自分类Dev

动作过滤器属性无法与Hub类一起正常使用

来自分类Dev

如何使动作过滤器使经过过滤的动作返回HttpNotFoundResult?

来自分类Dev

如何使动作过滤器使经过过滤的动作返回HttpNotFoundResult?

来自分类Dev

如何开发特定于动作过滤器的控制器或动作方法

来自分类Dev

将TempData从动作过滤器传递到动作

来自分类Dev

控制器上的同类型动作过滤器及其动作方法

来自分类Dev

控制器上的同类型动作过滤器及其动作方法

来自分类Dev

在MVC动作过滤器中获取类似于webapi的动作参数

来自分类Dev

如何在Web API中使用动作过滤器和HttpResponseMessage一起使用ETag

来自分类Dev

如何在ASP.NET CORE中将动作过滤器与依赖注入一起使用?

来自分类Dev

如何使用动作过滤器在asp.net mvc中集中进行模型状态验证?

来自分类Dev

如何为所有控制器编写动作过滤器

来自分类Dev

在MVC 5中的动作过滤器中调用异步方法

来自分类Dev

在MVC5中自动调用动作过滤器

来自分类Dev

如何在动作过滤器中获取当前模型

来自分类Dev

将会话变量存储在动作过滤器或静态方法中

来自分类Dev

将动作过滤器添加到局部视图

来自分类Dev

什么时候需要包括/排除MVC动作过滤器的基本调用?

来自分类Dev

Web API动作过滤器可处理空集和404

来自分类Dev

自定义动作过滤器统一依赖注入Web API 2

来自分类Dev

如何将JSon对象从动作过滤器类传递到Onsuccess脚本

来自分类Dev

动作过滤器中的ASP.NET Web API读取模型

来自分类Dev

在MVC5中自动调用动作过滤器

来自分类Dev

为什么我的动作过滤器不起作用?

Related 相关文章

  1. 1

    如何使用动作过滤器更改输入参数

  2. 2

    将服务注入动作过滤器

  3. 3

    生成由动作过滤器控制的PDF?

  4. 4

    阻止在动作过滤器中执行

  5. 5

    覆盖的动作是否从基本动作继承动作过滤器?

  6. 6

    动作过滤器属性无法与Hub类一起正常使用

  7. 7

    如何使动作过滤器使经过过滤的动作返回HttpNotFoundResult?

  8. 8

    如何使动作过滤器使经过过滤的动作返回HttpNotFoundResult?

  9. 9

    如何开发特定于动作过滤器的控制器或动作方法

  10. 10

    将TempData从动作过滤器传递到动作

  11. 11

    控制器上的同类型动作过滤器及其动作方法

  12. 12

    控制器上的同类型动作过滤器及其动作方法

  13. 13

    在MVC动作过滤器中获取类似于webapi的动作参数

  14. 14

    如何在Web API中使用动作过滤器和HttpResponseMessage一起使用ETag

  15. 15

    如何在ASP.NET CORE中将动作过滤器与依赖注入一起使用?

  16. 16

    如何使用动作过滤器在asp.net mvc中集中进行模型状态验证?

  17. 17

    如何为所有控制器编写动作过滤器

  18. 18

    在MVC 5中的动作过滤器中调用异步方法

  19. 19

    在MVC5中自动调用动作过滤器

  20. 20

    如何在动作过滤器中获取当前模型

  21. 21

    将会话变量存储在动作过滤器或静态方法中

  22. 22

    将动作过滤器添加到局部视图

  23. 23

    什么时候需要包括/排除MVC动作过滤器的基本调用?

  24. 24

    Web API动作过滤器可处理空集和404

  25. 25

    自定义动作过滤器统一依赖注入Web API 2

  26. 26

    如何将JSon对象从动作过滤器类传递到Onsuccess脚本

  27. 27

    动作过滤器中的ASP.NET Web API读取模型

  28. 28

    在MVC5中自动调用动作过滤器

  29. 29

    为什么我的动作过滤器不起作用?

热门标签

归档