无法将viewmodel中的对象列表设置为javascript变量

时间:2016-04-28 23:31:08

标签: javascript asp.net-mvc razor

如何将来自viewmodel的对象列表设置为javascript变量以在我的js文件中使用?我尝试设置它时遇到错误。

这是我的viewmodel

public class TargetListAddressesViewModel
{
    public List<Address> Addresses { get; set; }
    public int OrganizationId { get; set; }
}

这是地址对象

 public partial class Address   
{
    [Column] public int AddressId { get; set; }
    [Column] public bool IsPrimary { get; set; }
    [Column] public int ParentId { get; set; }
    [Column] public int ParentTypeId { get; set; }
    [Column] public int AddressTypeId { get; set; }
    [Column] public string Address1 { get; set; }
    [Column] public string Address2 { get; set; }
    [Column] public string City { get; set; }
    [Column] public string State { get; set; }
    [Column] public string PostalCode { get; set; }
    [Column] public string Country { get; set; }
    [Column] public string AddressHash { get; set; }
    [Column] public decimal Latitude { get; set; }
    [Column] public decimal Longitude { get; set; }
    [Column] public string ModifiedBy { get; set; }
}

这是我尝试将其设置为js变量的地方,但它会抛出一个错误,上面写着“VM320917:3 Uncaught SyntaxError:Unterminated template literal”

<script type="text/javascript">
  var organizationId = @Html.Raw(Model.OrganizationId);
  var addresses = @Html.Raw(Model.Addresses);
</script>

1 个答案:

答案 0 :(得分:0)

var addresses = @ Html.Raw(Json.Encode(Model.Addresses))