Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting A String Array

How can I handle an array of input

For example I have in my view:

<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);
}

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

like image 980
Sven van den Boogaart Avatar asked Feb 04 '15 15:02

Sven van den Boogaart


People also ask

What is string array in PostgreSQL?

String Array is one of the data types provided by PostgreSQL, array is a user-defined data type or built-in data type. PostgreSQL arrays play an important task in the database system, PostgreSQL provides a facility to define columns as an array with any valid data type, array should be integer [] type, character [] type.

How to use string array in Java?

When we create an array of type String in Java, it is called String Array in Java. To use a String array, first, we need to declare and initialize it. There is more than one way available to do so.

Can an array of characters be a string?

Just like arrays can hold other data types like char, int, float, arrays can also hold strings. In this case, the array becomes an array of ‘array of characters’ as the string can be viewed as a sequence or array of characters.

How to check if a string is present in an array?

In case you want to know if a certain string is present in the string array or not, then you can use the ‘contains’ method. For this, you need to first convert the string array into ArrayList as this method belongs to the ArrayList. The following implementation shows the use of the ‘contains’ method.


1 Answers

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

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);
}

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; }
}

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.

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

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
}
like image 99
Nick Avatar answered Oct 14 '22 05:10

Nick