PHP 数组可以通过 POST 方法在 ASP.NET MVC 中捕获吗?
PHP 数组可以通过 POST 方法在 ASP.NET MVC 中捕获吗?
查询字符串:WebAccess/ArrayTest?val[]=1&val[]=2&val[]=3
我写: ActionResult ArrayTest(String[] val)
但这仅在查询字符串删除“[]”时才有效
Can PHP array passed POST method to catch in ASP.NET MVC?
Query string: WebAccess/ArrayTest?val[]=1&val[]=2&val[]=3
I write: ActionResult ArrayTest (String[] val)
But this only works if the query string to remove the "[]"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASP.NET MVC 中内置的 DefaultModelBinder 无法处理 Rails/php/jquery1.4 样式的数组帖子(这就是您使用 val[]=1&val[]=2&val[]=3 所指的内容) )。
您必须创建一个自定义模型绑定器(谷歌搜索,有很多示例)或在括号内添加索引,如下所示:
并且索引不得有任何缺失的数字。
我已经用一个脚本修复了这个问题,在提交表单时仅添加索引。即在 jQuery 中:
The built in DefaultModelBinder in ASP.NET MVC can't handle rails/php/jquery1.4 style array posts (which is what you're referring to with val[]=1&val[]=2&val[]=3).
You have to either create a custom modelbinder (google it, lots of examples) or adding indices inside the bracket like so:
And the indices must not have any missing numbers.
I have fixed this with a script that on submit of the form just adds the indices. i.e. in jQuery:
(不是答案 - 但很难使用 SO 注释来解释这一点)
您认为这实际上与 PHP 有关,这让事情变得相当混乱。另外,您没有说明删除了哪个“[]”以使其正常工作。不知情的 ASP 程序员可能会发现它更容易理解:
使用某些 Web 开发语言,来自三个文本字段的数据随后可在名为“val”的数组中使用。如何在 ASP.NET 中复制这种行为?
C.
(not an answer - but its difficult to explain this using a S.O. comment)
You've rather confused things by suggesting that this is really anything to do with PHP. Also, you don't say which '[]' you removed to make it work. The unenlightened asp programmers out there might find it easier to understand:
Using some web development languages the data from each the three text fields is subsequently available in an array named 'val'. How to replicate this behaviour in asp.net?
C.
用法:
http://pavelsvetlov.blogspot.com/2016/04 /mvcnet-modelbinder-phpstyle-array.html
usage:
http://pavelsvetlov.blogspot.com/2016/04/mvcnet-modelbinder-phpstyle-array.html