How to add items in an Jquery array from an IENUMERABLE in .Net MVC?

2

How do I add items from a IEnumerable<T> to a JavaScript array?

View:

@using Colecao.ViewModels
@model ConfiguracoesViewModel

    @foreach (var equipamento in @Model.Equipamentos)
    { 
              //Gostaria de adicionar esses itens em um array JS.
    }
    
asked by anonymous 23.03.2015 / 13:08

1 answer

2

You can do this as follows:

var equipamentos = @Html.Raw(Json.Encode(Model.Equipamentos));

How to use?

You can usually use it as if it were any json.

Let's say my equipment model looks like this:

public class Equipamentos
{
    public string Nome { get; set;}
    public string Modelo { get; set;}
}

Its use will be as follows:

alert("Nome: "+ equipamentos[1].Nome + " | Modelo: "+ equipamentos[1].Modelo);
    
23.03.2015 / 13:53