Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 ASP.NET MVC 控制器中,我有两个创建和两个编辑操作。这些中的每一个都使用相同ViewBag的 s。问题是,如果我想改变一个ViewBag,我必须把它变成四个动作。有什么办法可以定义所有Viewbag的只是一个?
ViewBag
Viewbag
您可以编写一个自定义操作过滤器:
public class MyActionFilterAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.Controller.ViewBag.FooBar = "baz"; } }
然后用[MyActionFilter]属性装饰你所有的 4 个动作。
[MyActionFilter]