如何访问 FormCollection、ViewData 和 ModelState

发布于 2024-11-29 07:25:11 字数 176 浏览 1 评论 0原文

在 ASP.NET MVC 应用程序中运行时,除了直接在视图中工作之外,是否有任何方法可以访问当前正在运行的请求的 FormCollection、ViewData、ModelState 等?我希望能够从视图中调用一些自定义处理程序,但无需传递它们即可访问这些集合。我在想类似于网络表单中的 HttpContext.Current 的东西?

Is there any way to get access to the current running request's FormCollection, ViewData, ModelState, etc. when running in an ASP.NET MVC application other than if you are directly working in the View? I'd like to be able to call some custom handlers from within the view, but access these collections without having to pass them. I'm thinking something similar to HttpContext.Current in webforms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-12-06 07:25:11

尝试,

        var wrapper=new HttpContextWrapper(System.Web.HttpContext.Current);
        var routeData = RouteTable.Routes.GetRouteData(wrapper);
        Controller con = (Controller)ControllerBuilder.Current.GetControllerFactory().CreateController(new RequestContext(wrapper, routeData), routeData.Values["controller"].ToString());
        var viewData = con.ViewData;
        var modelState= con.ModelState;
        var form=new FormCollection();
        var controllerContext = new ControllerContext(wrapper, routeData, con);
        Predicate<string> propertyFilter = propertyName => new BindAttribute().IsPropertyAllowed(propertyName);
        IModelBinder binder = Binders.GetBinder(typeof(FormCollection));
        ModelBindingContext bindingContext = new ModelBindingContext()
        {
            ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => form, typeof(FormCollection)),
            ModelName = "form",
            ModelState = modelState,
            PropertyFilter = propertyFilter,
            ValueProvider = ValueProviderFactories.Factories.GetValueProvider(controllerContext)
        };
        form = (FormCollection)binder.BindModel(controllerContext, bindingContext);

Try,

        var wrapper=new HttpContextWrapper(System.Web.HttpContext.Current);
        var routeData = RouteTable.Routes.GetRouteData(wrapper);
        Controller con = (Controller)ControllerBuilder.Current.GetControllerFactory().CreateController(new RequestContext(wrapper, routeData), routeData.Values["controller"].ToString());
        var viewData = con.ViewData;
        var modelState= con.ModelState;
        var form=new FormCollection();
        var controllerContext = new ControllerContext(wrapper, routeData, con);
        Predicate<string> propertyFilter = propertyName => new BindAttribute().IsPropertyAllowed(propertyName);
        IModelBinder binder = Binders.GetBinder(typeof(FormCollection));
        ModelBindingContext bindingContext = new ModelBindingContext()
        {
            ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => form, typeof(FormCollection)),
            ModelName = "form",
            ModelState = modelState,
            PropertyFilter = propertyFilter,
            ValueProvider = ValueProviderFactories.Factories.GetValueProvider(controllerContext)
        };
        form = (FormCollection)binder.BindModel(controllerContext, bindingContext);
一页 2024-12-06 07:25:11

有一个 ViewContext 对象可以让您链接回您所要求的大部分内容,但您确实必须问自己为什么要在视图中执行所有这些操作。 (无论如何,恕我直言)

编辑:我可能误读了你的问题。控制器中有一个ControllerContext,视图中有一个ViewContext。 MVC 中的大多数扩展点都有某种 Context 对象,可让您获取请求及其数据。

There is a ViewContext object that lets you link back to most of what you're asking for, but you really have to ask yourself why you're doing all of this in the view. (IMHO anyway)

Edit: I may have misread your question. There is a ControllerContext in the controller and a ViewContext in the view. Most of the extensibility points in MVC have some sort of Context object that lets you get at the Request and it's data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文