如果表中没有记录,如何提供警报?

时间:2018-02-16 06:30:22

标签: 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>

控制器:

[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");
    }

2 个答案:

答案 0 :(得分:1)

如果响应数据是数组,

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

如果回复是对象,

{{1}}

答案 1 :(得分:0)

你可以这样使用

if (!data.GenList.length)
{
  alert("Nothing Found");
}