asp.net-mvc – 如何在使用类型化视图时在ActionFilterAttribute中设置模型数据
发布时间:2020-05-24 13:30:46 所属栏目:asp.Net 来源:互联网
导读:我使用强类型视图,其中所有ViewModel都继承了一个类BaseViewModel. 在装饰所有控制器的ActionFilter中,我想使用Model. 现在我只能像这样访问它: public override void OnActionExecuting(ActionExecutingContext filterContext) { ViewModelBase mode
|
我使用强类型视图,其中所有ViewModel都继承了一个类BaseViewModel. 在装饰所有控制器的ActionFilter中,我想使用Model. 现在我只能像这样访问它: public override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewModelBase model = (ViewModelBase)filterContext.ActionParameters["viewModel"];
base.OnActionExecuting(filterContext);
}
问题是,我必须知道密钥“viewModel”.关键是viewModel,因为在我的控制器中我用过:
是否有更安全的方式来访问模型? 解决方法OnActionExecuting在Action执行之前工作 – 因此Model被设置为null.您可以在OnActionExecuted中访问ViewData(或ViewData.Model):public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var model = filterContext.Controller.ViewData.Model as YourModel;
...
}
希望这可以帮助 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 编译器错误消息:编译器失败,错误代码为-532462766
- 403发布asp.net MVC后禁止
- asp.net – 让网站暂时无法使用的优雅方式?
- kendo-asp.net-mvc – 在Kendo Scheduler自定义模板中绑定D
- asp.net-mvc – ASP.NET MVC验证ViewState MAC失败
- ASP.NET Core 2.0 Razor vs Angular/React/etc.
- asp.net-mvc – 在MVC Ajax.ActionLink中传递多个参数
- asp.net-mvc – 如何将httppostedfilebase转换为String数组
- asp.net-mvc – 建立与SQL Server的连接时发生网络相关或特
- asp.net 结合mysql存储过程进行分页代码
推荐文章
站长推荐
- 是否可以使用ASP.NET ScriptManager来使用Window
- asp.net – 如何在.net 3.5中托管我的MVC3应用程
- asp.net-mvc – Razor ViewEngine HTML.Checkbox
- ASP.NET Core 1.0 ConfigurationBuilder().AddJs
- ASP.NET:Response.Redirect(…)后的代码会发生什
- ASP.NET:使用混合的HTML格式本地化内容
- 模型绑定 – WebApi2:自定义参数绑定以绑定部分
- ASP.NET代码隐藏中的当前工作目录 – 我们可以依
- asp.net – 如何MSDeploy构建的网站包到一个处女
- asp.net-mvc – 如何在ActionFilterAttribute AS
热点阅读
