Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how exactly should the 'myArray' array be passed to the mvc controller? I've tried everything but i can't seem to get anything to work

Controller

[HttpPost]
public ActionResult MyAction(MyModel model, List<string> myArray) {
     //code...
}

View

$('#dialog').dialog({
        //...
        buttons: {
            "Cancel": function () {
                $(this).dialog("close");
            },
            "Submit": function () {
                var arrCount= 0;
                var myArray = new Array();

                //grabs all the dropdownlists that begin with id 'dropdownlist' and adds it to 'myArray'
                $('form').contents().find("select[id ^= 'dropdownlist'] option:selected").each(function () {
                    myArray[arrCount++] = $(this).text();
                });

                if ($('form').validate().form()) {
                    $.ajax({
                        url: "MyController/MyAction",
                        type: 'POST',
                        dataType: "json",
                        traditional: true,
                        data: { 
                           model: $("form").serialize(),
                           myArray: myArray
                        },
                        success: function (result) {
                            alert("debug: complete");
                        }
                    });
                }
            }
        }
    });

I know how to pass in the array by itself to the controller. But, once i add my existing model into the equation, i'm unsure as to how i would be able pass my array to the controller. Any thoughts?

share|improve this question
Does the model get posted properly? – sormii Sep 7 '12 at 8:00
no, the model returns null on the controller – theStig Sep 7 '12 at 17:21

1 Answer

First of all, the simple solution is:

Make a string of array values with some separator,

1##2##3... or 1,2,3.. etc.

and use

public ActionResult MyAction(MyModel model, string myArray) {
     string[] values = myArray.Split("##"); //split incoming string with your separator

     List<int> myIntArray = new List<int>();
     foreach (var value in values)
     {
         int tmp = 0;
         if (int.TryParse(value, out tmp))
         {
             myIntArray.Add(tmp);
         }
     }

     //code...
}

works good for simple things.

Second approach is a little bit more complex but works for objects.

Suppose you have the following:

public class YourMainModel
{
    public YourMainModel()
    {
        ArrayField = new List<YourPartialModel>()
    }

    List<YourPartialModel> ArrayField {get; set;}

    //some other fields
}

public class YourPartialModel
{
    public string Name {get; set;}

    //some other fields
}

Use some kind of enumerator in your view to make the following:

<form id="myForm">

//some other fields from YourMainModel here

//dealing with our array
@for (int i = 0; i < Model.ArrayField.Count(); i++)
{
    @Html.TextBox("ArrayField[" + i + "].Name", Model.ArrayField[i].Name)

    //some other fields from YourPartialModel here
}

</form>

and then

[HttpPost]
public ActionResult MyAction(YourMainModel model) {
    //use model.ArrayFields here, it should be submitted
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.