John Edwards
John Edwards

Reputation: 1546

Sending select back to controller

I am trying to send a listbox back to my controller from my view. I dynamically add items to it with a text box and button, and I want to be able to send all of these items back to my view in some kind of array. How would I go about doing this?

I had the following modelcode:

[HttpPost]
    public ActionResult BasicIdentificationIndex(MyObject returndata, List<int> ints)

And then some input boxes:

<input type="text" name="ints" value="1" />
<input type="text" name="ints" value="4" />
<input type="text" name="ints" value="2" />
<input type="text" name="ints" value="8" />

This code works and is returned to my controller(not null).

EDIT: My issue is that I cannot get a select list to post back to my controller. I would like to send the following back to my controller:

 <select name="selectfrom" id="select-from" multiple size="5">
  <option value="String1">Item 1</option>
  <option value="String2">Item 2</option>
  <option value="String3">Item 3</option>
  <option value="String4">Item 4</option>
</select>

How would I go about doing this so that I can send a list of all the options( String1,String2,etc.) back to my controller? I have tried the following:

Controller:

 public ActionResult BasicIdentificationIndex(BasicIdentificationModel returndata,ICollection<String> AerialItems)

Model:

 public String AerialItems { get; set; }

View:

 <select name="AerialItems" id="select-to" multiple size="5">
      <option value="5">Item 5</option>
      <option  value="6">Item 6</option>
      <option  value="7">Item 7</option>
    </select>

But The item returned back to the controller is always null.

Upvotes: 1

Views: 634

Answers (2)

John Edwards
John Edwards

Reputation: 1546

Figured it out. I need to use Javascript to select all the items in the list. This will post them all back in the collection.

Upvotes: 1

Rikon
Rikon

Reputation: 2696

You should be able to just model bind back to a collection of ints...

I'm somewhat confused because this seemed to be copied from Haack's blog post on the subject... What you have listed should work, but if it's not could you include the rest of your code?

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Upvotes: 1

Related Questions