2015-11-27 41 views
2

所以我读了不同的堆栈Q /由于这个,但我仍然很困惑......修改值使用操作筛选

因此,当一个请求被发送到控制器它做一些处理,并发送一些结果回到一个看法。现在,就在它将数据发送到视图之前,我需要查看其数据并进行一些更改,然后让操作正常继续。我想修改自己的数据的方法

两个例子:然后

[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) 
    { 
    } 
} 
+0

请读[mcve]。 –

+0

@ArghyaC我现在会解决它。 – VSG24

+1

@ VSG24这里'ActionFilter'的好处是什么?为什么不简单地在业务逻辑或行为中执行该检查? –

回答

1

如果您可以更改行动或业务l逻辑,最好是把逻辑的行动或你的业务逻辑的方法,但是如果你不能更改或覆盖控制器方法,您可以使用filterContext.Controller.ViewData.Model创建ActionFilter,并覆盖OnActionExecuted和修改模型:

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); 
    } 
} 
+0

'filterContext.Controller.ViewData.Model = modifiedModel;'是我在找什么。 – VSG24

+1

就是这样,注意重写'OnActionExecuted'。我错误地在上面的评论中说了'OnActionExecuting'。 –