ASP.Net MVC - 将对象从控制器发送到视图再到控制器

发布于 2024-09-10 23:52:04 字数 409 浏览 3 评论 0原文

我刚刚开始使用 ASP.Net MVC 2,可能做错了什么。我有一个控制器,它构建一些对象并使用 ViewData 将它们传递到视图。在视图中,我显示数据等...然后想要将相同的数据(加上其他用户输入)提交回同一控制器。有什么简单的方法可以做到这一点吗?

如有必要,我将提供问题的更详细描述。

谢谢,美好的一天:)

编辑: 我阅读了有关 ViewModel 的更多内容,担心我没有正确使用它们,但显然它们不是解决方案。我的问题不是将数据获取到视图(我已经为此使用了视图模型),而是将数据返回到控制器。我使用复杂的对象,因此即使发送带有隐藏字段的表单也不是一个好的解决方案,因为它需要我序列化我的对象,这对于应该简单的任务来说太麻烦了。我现在要看看会议。

编辑2: 好的,我使用会话解决了问题,再简单不过了:)

I'm just starting with ASP.Net MVC 2 and might be doing something wrong. I have a controller who builds some objects and passes them to a view using ViewData. In the view I display the data etc ... and then want to submit the same data (plus other user input) back to the same controller. Is there any simple way to do this ?

I'll provide a more detailed description of the problem if necessary.

Thanks and good day :)

EDIT :
I read more on ViewModels fearing that I wasn't using them properly but apparently they're not a solution. My problem isn't getting data to the view (I already use a view model for that), but returning the data back to the controller. I use complex objects, so even sending a form with hidden fields wouldn't really be a good solution as it will require me to serialize my objects, which is too much hassle for a task that should be simple. I'm going to have a look at sessions for now.

EDIT 2:
Ok, I solved the problem using sessions, couldn't be easier :)

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

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

发布评论

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

评论(3

旧情别恋 2024-09-17 23:52:04

考虑使用以下任何解决方案来保存请求之间的状态:

  • 将数据保存在会话、缓存或 cookie 中。选择取决于数据是什么、数据在用户之间的变化程度以及数据的复杂程度。
  • 将这些项目写入隐藏输入。 Html.Hidden("foo", myData);

如果您选择写入隐藏输入,请考虑伪造 ViewState。但这不是最佳解决方案。

总的来说我更喜欢Session。不会被篡改,并且您可以握住复杂的物体。显然,Session 的缺点是未来的扩展性能、超时和多个会话的并发性。不过,其中一些问题很容易得到缓解。

Consider any of these solutions to keep state between requests:

  • keep that data in Session, Cache, or cookies. The choice will depend on what that data is, how variable the data is between users, and how complex it is.
  • write those items to hidden inputs. Html.Hidden("foo", myData);

If you choose to write to hidden inputs, consider faking ViewState. It's a non-optimal solution though.

I'd prefer Session overall. There's no tampering, and you can hold complex objects. Obviously the drawbacks to Session are future scaling performance, timeouts, and concurrency with multiple sessions. Some of those problems can easily be mitigated, though.

○闲身 2024-09-17 23:52:04

我有一个控制器可以构建一些
对象并将它们传递给视图
使用视图数据

错误:定义一个视图模型类并使您的视图强类型化到该模型,而不是使用 ViewData

渲染视图后,您将拥有一个表单,您应该将需要返回的所有内容放入此表单中:用户将操作的可见输入字段和包含您想要在操作中获取的任何上下文的隐藏字段发布到.

另一种选择是将此信息存储到 cookie 或会话中。

I have a controller who builds some
objects and passes them to a view
using ViewData

Wrong: define a view model class and make your view strongly typed to this model instead of using ViewData.

Once the view is rendered you will have a form and you should put everything you need to get back in this form: visible input fields that the user will manipulate and hidden fields that will contain any context you would like to get in the action you are posting to.

Another option is to store this information into cookies or session.

逆光下的微笑 2024-09-17 23:52:04

查看视图模型的一些示例。
这应该是反对使用 ViewData 的东西。

例如 链接

这样,您就处于整个链类型保存中,并且您可以在回发时接收整个模型作为参数。

Look at some examples of the View Model.
that should be favored against using the ViewData stuff.

e.g. Link

with that, you are on the whole chain typesave, and you can recieve the whole model as a parameter on the post back.

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