首页 > 解决方案 > 无法读取 null 的属性“createDocumentFragment”

问题描述

<div class="input-group col-md-6">
  <select id="select-mesaj-gonderim-turleri" class="form-control mesajgonderimturusec input-circle" asp- 
     for="Model.FKMesajGonderimTurleriID" asp-items="ViewBag.MesajGonderimTurleri" required>
  </select>
</div>
<div class="input-group col-md-6">
   <select id="select-birimler" class="form-control birimsec input-circle" asp-for="Model.FkBirimID" asp- 
         items="ViewBag.Birimler" required></select>
</div>

<script>
function GetBirimGetir() {
            var url = '@Url.Action("GetBirimlerByBirimKod", "Helper", new { area = "Yonetim" })';
            console.log("url", url);
            LoadSelectList(url, null, 'Seçiniz', 'select-birimler', null, false, ('@ViewBag.GuncellenecekBirimID' != null && '@ViewBag.GuncellenecekBirimID' > 0) ? '@ViewBag.GuncellenecekBirimID' : null, 'Birim Bulunamadı');
        };
        function GetMesajGonderimTurleriniGetir() {
            var url = '@Url.Action("GetMesajGonderimTurleriByID", "Helper", new { area = "Yonetim" })';
            console.log("url",url);
            LoadSelectList(url, null, 'Seçiniz', 'select-mesaj-gonderim-turleri', null, false,('@ViewBag.GuncellenecekMesajGonderimTurleriID' !== null && '@ViewBag.GuncellenecekMesajGonderimTurleriID' > 0) ? '@ViewBag.GuncellenecekMesajGonderimTurleriID' : 'null', 'Tür Bulunamadı');
        };

        $(document).ready(function () {
            $(document).load('change', '.birimsec', GetBirimGetir);
            $(document).load('change', '.mesajgonderimturusec', GetMesajGonderimTurleriniGetir);

        });
</script>

我正在使用 loadselectlist javascript 方法来填充下拉列表

当我进入插入页面时没有问题。下拉列表被填满。我可以选择并保存。进入更新页面时会抛出下图所示的错误

当我进入插入页面时没有问题。下拉列表被填满。我可以选择并保存。进入更新页面时会抛出下图所示的错误

在此处输入图像描述

function LoadSelectList(url, value, label, target, callback, selectAll, selecteds, actionEmptyLabel) {
    debugger;
    var targetDropDown = $('#' + target);
    console.log("actionEmptyLabel",actionEmptyLabel);
    
    $.getJSON(url, value, function (result) {

        if (result.Action != GLOBAL_VARIABLES.ActionSuccess) {
            Warning(result.Message);
            return false;
        }

        var items = result.Data;

        targetDropDown.empty();
        targetDropDown.removeAttr('disabled');
        
        if (items !== null && items.length > 0) {

            if (label !== null && label !== undefined) {
                targetDropDown.append($('<option/>', {
                    value: '',
                    text: label
                }));
            }
            console.log(" $(this)", $(this));
            console.log(" targetDropDown", targetDropDown);
            $.each(items, function (index, item) {
                console.log("index");

                if (item.Disabled) {

                    targetDropDown.append($('<option/>', {
                        value: item.Value,
                        text: item.Text,
                        disabled: "disabled"
                    }));
                } else {
                    targetDropDown.append($('<option/>', {
                        value: item.Value,
                        text: item.Text
                    }));
                }
            });
        } else {
            if (actionEmptyLabel !== null && actionEmptyLabel !== undefined) {
                targetDropDown.append($('<option/>', {
                    value: '',
                    text: actionEmptyLabel
                }));
            }
        }

        if (selectAll) {
            var selected = [];
            targetDropDown.find("option").each(function (i, e) {
                selected.push($(e).attr("value"));
            });
            targetDropDown.val(selected).trigger("change");
        }

        if (selecteds) {
            targetDropDown.val(selecteds).trigger("change");
        }
    }).fail(function (xhr, status, error) {
        console.error(xhr);
        console.error(error);

    });
}

标签: jqueryasp.net-coreasp.net-core-mvc

解决方案


尝试将模式附加到当前文档时出现相同的错误消息

$.get(url).done(function (data) {
   $(document).append(data).find('.modal').modal('show');
});

问题是不能附加到整页,而只能附加到正文

$.get(url).done(function (data) {
   $(document.body).append(data).find('.modal').modal('show');
});

推荐阅读