ASP.NET MVC 3 自定义操作过滤器 - 如何将传入模型添加到 TempData?
我正在尝试构建一个自定义操作过滤器,该过滤器从过滤器上下文中获取传入模型,将其添加到临时数据中,然后执行“其他操作”。
我的操作方法如下所示:
[HttpPost]
[MyCustomAttribute]
public ActionResult Create(MyViewModel model)
{
// snip for brevity...
}
现在,我想将 model
添加到 TempData
,在模型绑定启动并转换将值集合形成到 MyViewModel
中。
我该怎么做?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.Controller.ViewData.ModelState.IsValid)
return;
var model = filterContext.????; // how do i get the model-bounded object?
filterContext.TempData.Add(someKey, model);
}
I'm trying to build a custom action filter which grabs the incoming model out of the filter context, adds it to tempdata, then does "other stuff".
My action method looks like this:
[HttpPost]
[MyCustomAttribute]
public ActionResult Create(MyViewModel model)
{
// snip for brevity...
}
Now, i want to add the model
to TempData
, after the model-binding has kicked in and transformed the form value collection into MyViewModel
.
How do i do that?
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.Controller.ViewData.ModelState.IsValid)
return;
var model = filterContext.????; // how do i get the model-bounded object?
filterContext.TempData.Add(someKey, model);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
明白了 - 希望这是正确的做法:
Got it - hopefully this is the correct way of doing it: