Precargar select con knockout y web api .net

Lamento no ser muy específico, pero el tiempo que tengo ahora es poco:

Así sería:



//Clase representa un tipo identificacion
function TipoIdentificacion(data) {
    this.descripcion = ko.observable(data.descripcion);
    this.id = ko.observable(data.id);
}


function UsuarioViewModel(dataInicial) {
var self = this;
self.tiposIdentificacion = ko.observableArray();

// Carga inicial para llenar selects
    $.getJSON("/api/tiposidentificacion", function (allData) {
        var mappedTiposIdentificacion = $.map(allData, function (item) { return new TipoIdentificacion(item) });
        self.tiposIdentificacion(mappedTiposIdentificacion);
    });

}

Y en el html así:


<select id="ddltipoidentificacion" class="ddlregistro" data-bind="
                                                    options: tiposIdentificacion, 
                                                    optionsValue : 'id', 
                                                    optionsText: 'descripcion', 
                                                    optionsCaption: 'Seleccione...',
                                                    value : tipoIdentificacion" >
                                                </select>

Y en webapi:


public class TiposIdentificacionController : ApiController
    {
        // GET api/tiposidentificacion
        public IEnumerable<TipoIdentificacion> Get()
        {
            var sqlmanager = new SqlDataManager();
            return sqlmanager.GetTiposIdentificacion();
        }
    }


Sean felices! :) Y siéntanse libres de opinar ;)

No hay comentarios:

Publicar un comentario