我需要创建一个对象以发送给javascript函数,但是此信息来自我的viewbag。

new FleetSelector("False",
[{"OwnerId": "2df3893d-c406-48fe-8443-1622ddc51af2", "DisplayName": "String 1" },
{ "OwnerId": "7A402CD7-5EEA-4AF5-80A3-22EED0610C9D", "DisplayName": "String 2" }]);


在我的viewBag中,我有一个Fleets模型列表,该列表具有Guid OwnerId和String ownerName,这是我尝试过的方法,我知道这是错误的,但是请添加它来尝试解释我希望实现的目标

new FleetSelector("False",
                    [ @foreach (var fleet in ViewBag.Fleets)
                    {
                        '{' + '"' + "OwnerId" + '"' + ':' + '"' + fleet.OwnerId + '"' + ',' + '"' + "DisplayName" + '"' + ':' + '"' + fleet.ownerName + '"' + '}'
                    }  }]);

最佳答案

您必须在array对象之外填充FleetSelector

new FleetSelector("False",array);


这是数组定义:

var array=[];
@foreach (var fleet in ViewBag.Fleets)
{
   @:array.push({"OwnerId":@fleet.OwnerId,"DisplayName":@fleet.ownerName});
}

09-11 19:43