来自AJAX(ASP.NET MVC)的填充下拉列表



我在视图中有下拉列表

 @Html.DropDownList("Question1", null, "Вопрос 1", htmlAttributes: new {@class = "form-control", @style = "height:40px;margin-bottom: 20px;"})

我有显示所有问题的ajax调用

这里是

 function question_update() {
    $(".count").empty();
    $.ajax({
        url: '@Url.Action("QuestionsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
            var email = result;
            var edit = '@Url.Content("~/Images/Edit.png")';
            var delete_ = '@Url.Content("~/Images/Delete.png")';
            // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++) {
                var arrow  = '@Url.Content("~/Images/plus_plus.png")';
                var questionHtml = '<div class = "title" style="margin-top:15px;margin-left:15px;margin-bottom:10px;">'
                    +
                    '<img class="click" src="'
                    + arrow
                    + '">' +
                    '<span class="test">' +
                    '<input type="text" class="testclass" readonly value="' +
                    result[i].Quest + '">' +
                    '<a style="margin-left:25px;">' +
                    '<img src="' + edit + '"/>' +
                    '<img style="margin-left:10px;" src="' + delete_ + '"/>' +
                    '</div>' +
                    '<div class ="content" style="margin-left:60px; width: 80%; height: 100px; background: white;">' +
                    '<div style="width: 100%">' +
                    '<div style="float:left; width: 50%;">' +
                    '<b style="margin-left: 40px;">' + "Время на ответ"  + '</b>' +
                    '<b>' + ":" + '</b>' +
                    '<span>' + "Время на подготовку" +'</span>'+
                    '</div>' +
                    '<div style="float:right; width: 50%;">' +
                    '<b>' + result[i].TimeForAnswer + '</b>' +
                    '<b>' + ":" + '</b>' +
                    '<b>' + result[i].TimeForReady + '</b>'+
                    '</div>' +
                    '</div>' +
                    '</div>';
                $(".count").append(questionHtml);

在它的成功中,我需要更新下拉中的值

我像这样

  function question_update() {
    $(".count").empty();
    $.ajax({
        url: '@Url.Action("QuestionsList", "Questions")',
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        processData: false,
        success: function (result) {
            var email = result;
            var edit = '@Url.Content("~/Images/Edit.png")';
            var delete_ = '@Url.Content("~/Images/Delete.png")';
            // console.log(result[0].Name);
            for (var i = 0; i <= email.length - 1; i++) {
                var arrow  = '@Url.Content("~/Images/plus_plus.png")';
                var questionHtml = '<div class = "title" style="margin-top:15px;margin-left:15px;margin-bottom:10px;">'
                    +
                    '<img class="click" src="'
                    + arrow
                    + '">' +
                    '<span class="test">' +
                    '<input type="text" class="testclass" readonly value="' +
                    result[i].Quest + '">' +
                    '<a style="margin-left:25px;">' +
                    '<img src="' + edit + '"/>' +
                    '<img style="margin-left:10px;" src="' + delete_ + '"/>' +
                    '</div>' +
                    '<div class ="content" style="margin-left:60px; width: 80%; height: 100px; background: white;">' +
                    '<div style="width: 100%">' +
                    '<div style="float:left; width: 50%;">' +
                    '<b style="margin-left: 40px;">' + "Время на ответ"  + '</b>' +
                    '<b>' + ":" + '</b>' +
                    '<span>' + "Время на подготовку" +'</span>'+
                    '</div>' +
                    '<div style="float:right; width: 50%;">' +
                    '<b>' + result[i].TimeForAnswer + '</b>' +
                    '<b>' + ":" + '</b>' +
                    '<b>' + result[i].TimeForReady + '</b>'+
                    '</div>' +
                    '</div>' +
                    '</div>';
                $(".count").append(questionHtml);
                $.ajax({
                    url: '@Url.Action(" QuestionsList_new", "Questions")',
                    contentType: 'application/json; charset=utf-8',
                    type: 'GET',
                    dataType: 'json',
                    processData: false,
                    success: function (result) {
                        var $vacancy = $("#Question1");
                        $vacancy.empty();
                        $.each(result, function (a, b) {
                            $vacancy.append('<option value="' + b.Value + '">' + b.Text + '</option>');
                        });
                    }
                });
            }
        }
    });
}

,但我在下拉列表中看不到任何更新。

我的问题在哪里?

update

都可以。但是我有新的问题。在我的下拉列表中,我有undefined

<select class="form-control" id="Question1" name="Question1" style="height:40px;margin-bottom: 20px;"><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option><option value="undefined">undefined</option></select>

我如何解决这个问题?

基于您为result变量提供的示例数据从服务器返回:

{ ID = 1048, Quest = "тест" }

您的each循环需要看起来像:

$.each(result, function (a, b) {
  $vacancy.append('<option value="' + b.ID + '">' + b.Quest + '</option>');
});

您试图访问的属性" text"one_answers" value"在数据中不存在,因此为什么您会获得"未定义"。您需要使用所涉及的实际属性的名称。

最新更新