所以我读了不同的堆栈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)
{
}
}
请读[mcve]。 –
@ArghyaC我现在会解决它。 – VSG24
@ VSG24这里'ActionFilter'的好处是什么?为什么不简单地在业务逻辑或行为中执行该检查? –