简体   繁体   English

C# ASP.NET MVC 5 ViewModel 问题

[英]C# ASP.NET MVC 5 ViewModel issue

I have a ViewModel defined, that represents the required properties.我定义了一个 ViewModel,它代表了所需的属性。 Using this ViewModel to define the view with razor.使用此 ViewModel 定义具有 razor 的视图。

The HttpPost action gets the ViewModel as parameter. HttpPost 操作获取 ViewModel 作为参数。 I can see all parameters in the POST command if check with Request.Form.AllKeys and also in the action argument, that is the ViewModel.如果使用 Request.Form.AllKeys 和 action 参数(即 ViewModel)检查,我可以看到 POST 命令中的所有参数。

If I change the ViewModel class, ie take some properties out, or add some properties and use them in the view all works fine.如果我更改 ViewModel class,即取出一些属性,或者添加一些属性并在视图中使用它们,一切正常。

Only one boolean property I can not remove, without leaving a hidden input field with the id of that property in the view.只有一个 boolean 属性我无法删除,除非在视图中留下带有该属性 ID 的隐藏输入字段。

If I remove it, the ViewModel argument in the HttpPost action doesn't get the changes of all boolean properties of the ViewModel when clicked those checkboxes in the view, but I can see with Request.Form.AllKeys all parameters in the POST command.如果我删除它,则 HttpPost 操作中的 ViewModel 参数在单击视图中的这些复选框时不会获得 ViewModel 的所有 boolean 属性的更改,但我可以使用 Request.Form.AllKeys 看到 POST 命令中的所有参数。

I don't understand what happens there.我不明白那里发生了什么。

I 've found out what has caused the error.我发现了导致错误的原因。 The javascript code, that is handling the clicks of the checkboxes, still contained the function for the already removed ID.处理复选框点击的 javascript 代码仍然包含已删除 ID 的 function。 That forced the javascript code to stop running the other functions, and the other checkboxes kept the old status==false.这迫使 javascript 代码停止运行其他功能,并且其他复选框保持旧状态==false。 So the values of those properties in the ViewModel never changed.所以 ViewModel 中这些属性的值永远不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM