0

I'm trying to pass an array from javascript back into my MVC app. There is a console.log(selected) in the javascript and it outputs an array of data:

["<img src="https://owlexpress.kennesaw.edu/stugifs/open.gif">", "MATH 1190/01 - Calculus I", "84528", "       4.000", "Full Term", "40", "34", "6", "0", "0", "0", "Kennesaw Campus", "Classroom - 100%", "Mathematics &amp; StatisticsRoom 108", "8:00 am - 9:10 amLecture", "Aug 17, 2015", "Dec 14, 2015", "Xiaohui Rebecca  Shi (P)", ""]

But when the breakpoint in the controller hits I see that selectedClassesArray == null

Javascript:

$('#WatchButton').on('click', function (e) {
    var selected = [];
    $("tr.selected").each(function () {
        $(this).find('td').each(function(){
            selected.push($(this).html());                
        });
    });
    console.log(selected);
    $.ajax({
        type: "POST",
        url: "/ClassSearch/watchClasses",
        // I have tried setting data both ways with the same result:
        //data: { selectedClassesArray: selected },
        data: [selected],
        traditional: true,
        success: function (data) { alert("SUCCESS"); }
    });
});

ViewModel:

public class SelectedClassesArray
{
    public string[] arrayOfClasses { get; set; }
}

Controller ACtion:

//Had to set ValidateInput to false since one of the strings contains < (it errored without this)
[HttpPost, ValidateInput(false)]
    public ActionResult watchClasses(IEnumerable<SelectedClassesArray> selectedClassesArray)        
    { //Breakpoint here shows that selectedClassesArray is null
        foreach (var item in selectedClassesArray)
        {
            Console.WriteLine(item.ToString());
        }
        return View();
    }

2 Answers 2

2

Your action is expecting a List of arrays:

watchClasses(IEnumerable<SelectedClassesArray> selectedClassesArray)  

You probably want to get only the array, which is in the view model:

watchClasses(SelectedClassesArray selectedClassesArray)  

And what @taxicala said makes sense, try passing only the array, not an array inside another array:

data: { selectedClassesArray: selected }

The posted property name must match the view model parameter's name.

Sign up to request clarification or add additional context in comments.

7 Comments

Using data: selected lets me then see the property arrayOfClasses but it's still empty as seen here http://imgur.com/Eg0hsiP
@Jrow but did you change the action params as I said ?
Yes, it's [HttpPost, ValidateInput(false)] public ActionResult watchClasses(SelectedClassesArray selectedClassesArray) { return View(); } now
@Jrow ok, I'm almost sure that if you use watchClasses(string[] arrayOfClasses) it will work. I'm afraid for using that class as parameter you would change a little you data to post.
@Jrow how it worked, with watchClasses(string[] arrayOfClasses) ?
|
0

Did you try:

$.ajax({
    type: "POST",
    url: "/ClassSearch/watchClasses",
    // I have tried setting data both ways with the same result:
    //data: { selectedClassesArray: selected },
    data: selected,
    traditional: true,
    success: function (data) { alert("SUCCESS"); }
});

Or perhaps the traditional serialization is failing:

$.ajax({
    type: "POST",
    url: "/ClassSearch/watchClasses",
    // I have tried setting data both ways with the same result:
    //data: { selectedClassesArray: selected },
    data: { selectedClassesArray: selected },
    traditional: false,
    success: function (data) { alert("SUCCESS"); }
});

1 Comment

When I use data: { selectedClassesArray: selected} I get this http://imgur.com/IvaUNNp and when I use data: selected I get this http://imgur.com/Eg0hsiP so it seems like data: selected is working "better"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.