Post array in ASP.NET MVC 2 using name=“array_name[]” for form elements

你。 提交于 2019-12-04 12:53:02

问题


Good day!

In PHP it is possible to assign name attribute to input elements with square brackets, like this: name="my_value[]" and PHP automagically converts this to array on server side.

Is this possible in ASP.NET MVC? If not is there any alternative to process a bunch of checkboxes in ASP.NET MVC?

Thanks in advance!


回答1:


Yes, it is possible. You might take a look at the following blog post about the convention used by the default model binder.




回答2:


Make sure the name is still the same, but go ahead and remove the brackets. You can then add the values to an array like so:

string[] values = Request.Form.GetValues("my_value");
foreach (string value in values) {
   ...
}


来源:https://stackoverflow.com/questions/4871609/post-array-in-asp-net-mvc-2-using-name-array-name-for-form-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!