javascript - 当表中没有记录时如何提供警报?

标签 javascript jquery asp.net asp.net-mvc

当我单击提交按钮时,基于隐藏字段 ID,我们从数据库中获取值并加载到表中。如果值存在,则应加载表,否则我需要将警报显示为“没有记录”。当前值已加载到表中,但在没有记录时不显示警报。

<a href="javascript:void(0)" id="InteractGeneric"><b>Interact Generic List</b></a>

<div class="table-responsive" id="findValue" style="display:none;height:500px;">
                        <table id="example" class="display table table-striped table-bordered" cellspacing="0" width="100%;">
                            <thead>
                                <tr>
                                    <th>S#</th>
                                    <th>Generic Name</th>
                                    <th>Interact Generic</th>
                                    <th>Interact Description</th>

                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>

脚本:

<script>
        $(document).ready(function () {
            $('#InteractGeneric').click(function () {
                var GenericID = $("#hdnGenericID").val();
                $("#example tbody tr").remove();

                $.ajax({

                    type: 'POST',

                    url: '@Url.Action("GenericInteractionNewDetails")',
                    dataType: 'json',
                    data: { GenericID: GenericID },
                    success: function (data) {
                        var items = '';
                        $.each(data.GenList, function (i, item) {
                            if (data.GenList == undefined)
                            {
                                alert("Nothing Found");
                            }

                            $("#findValue").show();

                            var rows = "<tr>"
                            + "<td>" + (i + 1) + "</td>"
                            + "<td>" + item.GenericName + "</td>"
                            + "<td>" + item.GenSuperClass + "</td>"
                            + "<td>" + item.GenClass + "</td>"
                            + "</tr>";
                            $('#example tbody').append(rows);
                        });
                    },
                    error: function (ex) {
                        var r = jQuery.parseJSON(response.responseText);
                        alert("Message: " + r.Message); d
                        alert("StackTrace: " + r.StackTrace);
                        alert("ExceptionType: " + r.ExceptionType);
                    }
                });
                return false;
            });
        });

    </script>

Controller :

[HttpPost]
        public ActionResult GenericInteractionNewDetails(int GenericID)
        {
            List<GenericMaster> GenList = dGMSP.GenericInteractionDetails(GenericID);
            var data = new { GenList = GenList };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

警报不起作用:

if (data.GenList == undefined)
    {
      alert("Nothing Found");
    }

最佳答案

如果响应数据是一个数组,

    if (data.GenList.length === 0) {
       alert("No items found")
    }

如果响应是一个对象,

   if (jQuery.isEmptyObject(YOUR_OBJECT)) {
       alert("No item found")
   }

关于javascript - 当表中没有记录时如何提供警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48821246/

相关文章:

c# - 如何通过点击按钮传递参数

c# - 单击按钮后,工具提示计算了 asp 按钮的链接

javascript - 在从父类继承的函数中引用子类

javascript - 翻转 div 的内容在 Firefox 上有效,但在 Chrome 中无效

javascript - 当文本超过框大小时,textarea 不滚动

javascript - 获取顶部框架的URL

c# - 使用ajax更改div文本不起作用

asp.net - gridview 标题中的下拉列表和 rowdatabound 列更改

javascript - 将 map 标记置于 map 中心?

javascript - 如何将获取的数据放入变量中