C# MVC 4: Passing JavaScript array in View to Controller

前端 未结 3 1841
南旧
南旧 2021-02-13 22:24

In MVC 4, how do you pass a JavaScript array in the View to a function in the Controller with AJAX?

This doesn\'t seem the work:

$.ajax(
        {
               


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-13 23:12

    your Ajax :

    $.ajax({
        type: "POST",
        url: "../Home/SaveTable",
        contentType: 'application/json',
        data: {function_param: JSON.stringify(countryArray)},
    });
    

    in your controller:

    using Newtonsoft.Json;
    
        public string SaveTable(string function_param)
        {
           dynamic func_param = JsonConvert.DeserializeObject(function_param)
        }
    

    then you will be able to do a foreach in your controller.

提交回复
热议问题