发布字符串数组

编程入门 行业动态 更新时间:2024-10-26 12:29:05
本文介绍了发布字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何处理输入数组

例如我在我看来:

<input type="text" name="listStrings[0]" /><br /> <input type="text" name="listStrings[1]" /><br /> <input type="text" name="listStrings[2]" /><br />

在我的控制中,我尝试获得如下值:

In my control I try to get the values like:

[HttpPost] public ActionResult testMultiple(string[] listStrings) { viewModel.listStrings = listStrings; return View(viewModel); }

在调试时我可以看到 listStrings 每次都是 null 。

On debugging I can see listStrings is null every time.

为什么它为null并且如何获取输入的值数组

Why is it null and how can I get the values of the input array

推荐答案

使用ASP.NET MVC发布基元集合

要发布基元集合,输入必须具有相同的名称。这样,当您发布表单时,请求的正文将显示为

Posting a Collection of Primitives With ASP.NET MVC

To post a collection of primitives the inputs just have to have the same name. That way when you post the form the request's body will look like

listStrings=a&listStrings=b&listStrings=c

MVC会知道,由于这些参数具有相同的名称,因此应将它们转换为集合。

MVC will know that since these parameters have the same name they should be converted to a collection.

因此,将表单更改为这样

So, change your form to look like this

<input type="text" name="listStrings" /><br /> <input type="text" name="listStrings" /><br /> <input type="text" name="listStrings" /><br />

我还建议您将控制器方法中的参数类型更改为 ICollection< string> 而不是 string [] 。所以你的控制器看起来像这样:

I would also recommend changing the parameter type in your controller method to be an ICollection<string> instead of a string[]. So your controller would look like this:

[HttpPost] public ActionResult testMultiple(ICollection<string> listStrings) { viewModel.listStrings = listStrings; return View(viewModel); }


发布一个集合更复杂的对象

现在,如果您想发布更复杂对象的集合,请说一个 ICollection< Person> 你的 Person 类的定义是

public class Person { public string Name { get; set; } public int Age { get; set; } }

然后您在原始表单中使用的命名约定将起作用。由于您现在需要多个表示不同属性的输入来发布整个对象,因此只需命名具有相同名称的输入就没有意义。您必须指定输入在名称中指定的对象和属性。为此,您将使用命名约定 collectionName [index] .PropertyName 。

then the naming convention you used in your original form would come into play. Since you will need multiple inputs representing different properties to post the whole object now, just naming the inputs with the same name won't make sense. You will have to specify which object and which property an input represents in the name. For this you will use the naming convention collectionName[index].PropertyName.

例如年龄 人物的属性可能有像这样的名字[0] .Age 。

For instance an input for the Age property of a Person might have a name like people[0].Age.

在这种情况下,用于提交 ICollection< Person> 的表格如下所示:

A form used to submit an ICollection<Person> in this case would look like:

<form method="post" action="/people/CreatePeople"> <input type="text" name="people[0].Name" /> <input type="text" name="people[0].Age" /> <input type="text" name="people[1].Name" /> <input type="text" name="people[1].Age" /> <button type="submit">submit</button> </form>

预期请求的方法如下所示:

The method expecting the request would look something like this:

[HttpPost] public ActionResult CreatePeople(ICollection<Person> people) { //Do something with the people collection }

更多推荐

发布字符串数组

本文发布于:2023-10-11 13:46:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!