发布一个字符串数组 [英] Posting A String Array

查看:21
本文介绍了发布一个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理输入数组

例如我认为:

<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.

为什么它为空以及如何获取输入数组的值

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[].所以你的控制器看起来像这样:

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 类的定义是


Posting a Collection of More Complex Objects

Now, if you wanted to post a collection of more complex objects, say an ICollection<Person> where your definition of the Person class was

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.

例如,PersonAge 属性的输入可能具有类似 people[0].Age 的名称.

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

在这种情况下,用于提交 ICollection 的表单如下所示:

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
}

这篇关于发布一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆