回发值丢失!
我有一个带有典型create方法的控制器(一个用于GET,一个用于POST)。 POST 采用强类型参数:
[HttpPost] public ActionResult Create(Quiz entity)
但是,当进行回调时,我的实体的属性为 null...如果我像这样重新定义它:
[HttpPost] public ActionResult Create(Quiz entity, FormCollection form)
我可以看到值在那里,例如 form["Component "]
包含 “1”
。我过去没有遇到过这个问题,我不明白为什么这个类会有所不同。
有人有什么想法吗?
I have a controller with typical create methods (one for GET, one for POST). the POST takes a strongly-typed parameter:
[HttpPost] public ActionResult Create(Quiz entity)
however, when the callback is made, the properties of my entity are null... if I redefine it like this:
[HttpPost] public ActionResult Create(Quiz entity, FormCollection form)
I can see that the values are there e.g. form["Component"]
contains "1"
. I've not had this problem in the past and I can't figure out why this class would be different.
thoughts anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出来了!
因此,在我的模型中(请参阅下面对 @ataddeini 线程的评论),您可以看到我有一个组件...来表示我使用了几个列表框的组件,第二个(组件)依赖于第一个(产品)的内容。在生成第二个列表时,我使用
它(如上面的链接之一所示)生成一个名为“Component”的表单字段......这就是问题所在。我需要做的是将它绑定到组件的 Id!
欢呼!
I FIGURED IT OUT!!
so, in my model (see comments on @ataddeini's thread below) you can see I have a Component... to represent components I used a couple of listboxes, the second (Components) dependent on the contents of the first (Products). In generating the second list I used
which (as shown in one of the above links) generates a form field called "Component"... and therein lies the problem. What I needed to have done is bind it to the the Id of the component instead!
hurray!
让默认模型绑定器在回发时为您实例化
Quiz
的最简单方法是在您的视图中使用 Html 表单助手。因此,例如,如果您的Quiz
类如下所示:视图中的以下代码将确保回发时出现值:
请记住,需要回发但未显示在中的值视图(如标识符)需要使用
Html.HiddenFor
添加到视图中。这是 Html 表单辅助函数的更全面列表。
The easiest way to get the default model binder to instantiate
Quiz
for you on postback is to use the Html form helpers in you view. So, for example, if yourQuiz
class looked like this:The following code in your view would ensure the values are present on postback:
Keep in mind that values which need to be posted back but not shown in the view (like identifiers) need to be added to the view with
Html.HiddenFor
.Here's a more comprehensive list of Html form helper functions.