Ivin Raj
Ivin Raj

Reputation: 3429

how to provide an alert when there is no record in the table?

When i click on submit button, Based on hidden field ID we are getting the value from database and loaded in the table. if value is present the table should be loaded otherwise i need to display alert as 'there is no record'.Currently the value is loaded in the table but it is does not show an alert when there is no record.

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

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

Alert does not work:

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

Upvotes: 2

Views: 758

Answers (2)

SilentCoder
SilentCoder

Reputation: 2000

If the response data is an array,

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

If the response is an object,

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

Upvotes: 1

Nitesh Kumar
Nitesh Kumar

Reputation: 1774

You can use like this

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

Upvotes: 0

Related Questions