没有显示select中的数据

Doesn't show data in the select

本文关键字:数据 select 显示      更新时间:2023-09-26

在我的输入中没有显示任何内容。

这是我的脚本函数在我的视图:

<script>
var Caracteristicas = [];
function LoadCaracteristicas(element) {
    if(Caracteristicas.length === 0)
    {
        $.ajax({
            url:'@Url.Action("GetCaracteristicas","Inspeccion")',
            type: 'GET',
            cache: false,
            dataType: 'json',
            success: function(data){
                Caracteristicas = data;
                alert(data);
                renderCaracteristica(element)
            },
            error: function (e) {
                console.log(e)
            }
        });
    }
    else
    {
        renderCaracteristica(element);
    }
}
function renderCaracteristica(element) {
    var $ele = $(element);
    $ele.empty();
    $ele.append($('<option/>').val('0').text('Select'));
    $.each(Caracteristicas, function (i, val) {
        $ele.append($('<option/>').val(val.Id_Caracteristica).text(val.Descripcion));
    })
}

My Select where I want to show data:

<table class="table table-responsive">
    <tr>
        <td>Caracteristica</td>
        <td>Resultado</td>
        <td>&nbsp;</td>
    </tr>
    <tr class="mycontainer" id="mainrowCateristica">
        <td>
            <select id="IDCateristicas" class="form-control"> ----- Show Data
                <option>Select</option>
            </select>
            <span class="error">Seleccione una Caracteristica</span>
        </td>
        <td>
                <input type="radio" id="RadioOK" name="resultado" value="1"> OK<br>
                <input type="radio" id="RadioNOK" name="resultado" value="0"> NOK<br>
        </td>
        <td>
            <input type="button" id="BtnAdd" value="Agregar" style="width:80px" class="btn btn-success" />
        </td>
    </tr>
</table>

My Controller Function:

public JsonResult GetCaracteristicas()
    {
        CalidadEntities db = new CalidadEntities();
        var data = from c in db.Caracteristicas select c;
        return Json(data.ToList(), JsonRequestBehavior.AllowGet);
    }

控制器假设它在JsonResult函数中传递数据,但在select中它没有显示任何东西。

我一开始就认为:

<script type="text/javascript">
            window.onload = function () {
                LoadCaracteristicas($("#IDCateristicas"));
            };

我终于明白了。我改变我的控制器的功能:

 public JsonResult GetCaracteristicas()
    {
        int tipoc = 10; -- Temporaly
        CalidadEntities db = new CalidadEntities();
        var data = (from c in db.Caracteristicas where c.Id_TComponente == tipoc
                    select new {
                        Id_Caracteristica =c.Id_Caracteristica,
                        Descripcion = c.Descripcion
                    }).ToList();
        return Json(data, JsonRequestBehavior.AllowGet);
    }

我不知道为什么,但这对我来说是唯一可行的方法。