如何ViewBag数据后,表单POST得救? [英] How can ViewBag data be saved after a form post?

查看:236
本文介绍了如何ViewBag数据后,表单POST得救?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 ViewBag.Something 并随机生成该数据。在我看来,我设置为一个标签像这样 @ Html.LabelFor(M = GT; m.Something,(串)ViewBag.Something)。这个工程搞好,但是当我提交表单可能有错误,如果有错误,我需要这个标签保持不变,我不想动态/随机数据了,所以我不认为这是发生在控制器方法这ViewBag。有没有办法留住这个值,而不必在我的控制器中的一些私有变量?这MVC /剃刀做它的一些好办法?

So I have a ViewBag.Something and this data is randomly generated. In my view, I set this to a label like so @Html.LabelFor(m => m.Something, (string)ViewBag.Something). This works out well but when I submit the form there could be errors and if there are errors, I need this label to remain the same, I don't want dynamic/random data anymore so I wouldn't call the controller method that generated this ViewBag. Is there a way to retain this value without having some private variable in my controller? Some nice way that MVC/Razor does it?

推荐答案

在弄,建立一个模型,将它设置动态当返回查看()是正在执行中,执行返回查看(模型)。然后在视图中建立一个隐藏字段,可以继续传递所需的价值。我选择了走这条路,因为我不担心任何服务器code键使一个帖子这项工作,我不担心任何会话。

In the get, set up a model, set it dynamically and when return view() is being executed, do return view(model). Then in the view, set up a hidden field that can keep on passing the value needed. I chose to go this route because I don't have to worry about any server code to make this work on a post and I don't have to worry about any sessions.

这篇关于如何ViewBag数据后,表单POST得救?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆