Skip to content Skip to sidebar Skip to footer

How To Pass Array To Mvc Controller With Jquery?

I am beginner to develope .Net MVC 5 application. But I have some problem with passing array or object to controller with Jquery. I'm adding dynamically input fields with button.

Solution 1:

You need to have a strongly typed object.

JavaScript

$("#getButtonValue").click(function (e) {
    e.preventDefault();
    var list = []; 
    for (var i = 1; i < counter; i++) { 
        list.push($('#textbox' + i).val());
    } 
    var postData = { values: list }; 
    $.ajax({ 
        type: "POST", 
        url: "/Surveys/PostQuestionAndOptions", 
        data: postData, 
        success: function (data) { 
            alert(data); 
        }, 
        dataType: "json", 
        traditional: true 
    }); 
});

Strongly typed object

public MyValues {
    public list<string> values {get; set;}
}

Controller method

[HttpPost] 
public JsonResult PostQuestionAndOptions(MyValues model) { 
    return Json(true, JsonRequestBehavior.AllowGet); 
}

Post a Comment for "How To Pass Array To Mvc Controller With Jquery?"