将ViewBag(具有int类型列表)传递给Controller

时间:2015-06-15 17:16:27

标签: c# jquery asp.net-mvc jquery-ui-dialog

我试图在jquery load上将int类型的列表传递给Controller,但是没有成功。 这是我的代码:

function Addcov() {
    var dt = '@ViewBag.dt';
    @{
        List<int> covtypes = new List<int>();
        foreach (var item in ViewBag.CovTypes)
        {
            covtypes.Add(item);
        }
    }
    alert('@covtypes');

    $("#Form").dialog({
         autoOpen: true,
         width: 1000,
         resizable: false,
         title: 'Add',
         modal: true,
         open: function () {
             $(this).load(
                 '../controller/AddAction', 
                 { fromDate: dt, CovTypes: JSON.stringify('@covtypes') },
                 function (response, status, xhr) {});
             },
             buttons: {}
        });
    }

3 个答案:

答案 0 :(得分:0)

我可能会看到你的问题,路径。调试并确保您传递的路径是正确的,并确保数据实际通过covtypes传递。

我在这里看到的主要问题是路径,它未正确传递。 尝试完整的路径,看看会发生什么。

答案 1 :(得分:0)

如果您已在控制器上编写了调用方法,那将会很有帮助,但在您的情况下,您发送了Get请求,因此,您可能会指出此值来自URI,如下所示:

public ActionResult AddAction(object fromDate, [FromUri] List<int> arr)

答案 2 :(得分:0)

我可以建议将ViewBag [List of int]的值作为csv放在html元素中,然后将它们转换为int数组并传递给控制器​​

<input type="hidden" id="list" value="@ViewBag.CovTypes.Aggregate((x,y)=>x+","+y)" />

function Addcov() {
 var data=$("#list").val().split(",")map(function(i,e){   return parseInt(e);          });

$("#Form").dialog({
     autoOpen: true,
     width: 1000,
     resizable: false,
     title: 'Add',
     modal: true,
     open: function () {
         $(this).load(
             '../controller/AddAction', 
             { fromDate: dt, CovTypes:JSON.stringify(data) },
             function (response, status, xhr) {});
         },
         buttons: {}
    });
 }