如何处理 ASP.NET MVC 中控制器和视图之间的共享数据?

发布于 2024-10-07 05:19:24 字数 429 浏览 0 评论 0原文

我们有一个模型(例如,List)。构建列表的函数是不确定的,并且在请求的生命周期内需要在控制器和视图中引用输出。由于它是每个请求的,因此它不能是静态的或单例的。

它是一个通用结构,可以从任何视图或控制器引用。

由于我们无法从视图访问控制器(原则上,我们同意),因此我们无法将其保留在控制器中。我们目前将其保存在 ViewData 字典中,并在控制器或视图中初始化它(如果控制器不需要它)。

我们认为使用 ViewData 来实现此目的可能并不理想,因为它最初并不是为了供控制器使用而创建的。有没有更好的方法在控制器和视图之间共享公共的每个请求数据?如果没有,我们将继续使用 ViewData

有 HttpContext.Items 字典,但我不确定它是否适合此目的。

We have a model (say, List<string>). The function that builds the list is non-deterministic and the output is needed to be referenced in both controller and the view during the lifetime of the request. Since it's per-request, it cannot be static or singleton.

It's a common structure and it can be referenced from any view or controller.

Since we can't access controller from the view (by principle, and we agree), we cannot keep it in the controller. We're currently keeping it in the ViewData dictionary and initialize it in the controller, or the view (if the controller didn't need it).

We think that using ViewData for this purpose may not be ideal since it's not created to be consumed by a controller in the first place. Is there a better way to share common per-request data between Controller and the View? If not we'll stick with ViewData.

There is HttpContext.Items dictionary but I'm not sure if it fits to this purpose.

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

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

发布评论

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

评论(3

烟织青萝梦 2024-10-14 05:19:24

在请求的生命周期内,需要在控制器和视图中引用输出

MVC 的工作方式,执行控制器中的操作代码,并将结果数据传递到使用信息绘制页面的视图引擎您通过调用 View(data) 或在 ViewData 字典中传递。

我不知道你想做什么,但听起来这更像是一个糟糕方法的问题,而不是技术问题(不过我可能是错的)。

您能解释一下为什么在渲染视图时需要控制器吗?如果您需要与 List 关联的任何逻辑(处理它或对其执行任何操作),我只需创建一个扩展 List的新类,将逻辑添加到该类而不是控制器,然后传递该对象的对象类到视图,使用 View() 或 ViewData[]。

the output is needed to be referenced in both controller and the view during the lifetime of the request

The way MVC works, the Action code in the Controller is executed, and the resulting data is passed to the view engine that draws the page using the info you passed either with the call to View(data) or in the ViewData dictionary.

I don't know what you are trying to do, but it sounds like it's more a problem of a bad approach than a technical one (I might be wrong, though).

Could you explain why you need the controller while the View is rendered? If you need any logic associated with the List (to process it or do anything with it) I would just create a new class that extends List<T>, add the logic to that class instead of the controller, and pass an object of that class to the View, either using View() or ViewData[].

无力看清 2024-10-14 05:19:24

您想要做的具体事情是什么?

似乎您只是询问如何将一些数据从控制器传递到视图,这是相当简单的任务。只需使用 ViewData,是的,或者在 MVC3 情况下使用 ViewBag 或使用 ViewModel。

或者有什么特殊情况? “从控制器和视图引用”是什么意思?数据从哪里来?通常情况是控制器为视图准备数据并将其作为 ActionResult(或者更好,作为 ViewModel)传递。视图永远不应该绕过控制器自行获取一些数据。

What is exact thing you're trying to do?

Seems like you just asking about the way to pass some data from the Controller to the View which is rather trivial task. Just use ViewData, yes, or ViewBag in MVC3 case or use ViewModels.

Or there is somewhat special case? What does "referencing from Controller and from View" mean? Where the data is coming from? Usually the case is that Controller prepares data for the View and passes it as an ActionResult (or better, as a ViewModel). View should never take some data on its own bypassing the controller.

笑梦风尘 2024-10-14 05:19:24

控制器操作应始终首先调用。如果您有多个控制器调用相同的视图/部分视图,那么您应该将代码重构为一种方法并调用该方法。

ViewData 是执行此操作的解决方案,如果您确实想要“一次访问”类型信息,那么可能是 TempData,但 ViewData 就是为此而设计的。

Controller action should always be called be first. If you have multiple controllers calling the same view/partial view then you should be refactoring the code to one method and call that.

ViewData is the solution to do this, if your really wanting "once access" type information then maybe TempData but ViewData is designed for this.

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