mercredi 7 septembre 2016

If condition in mvc by Jqurey

I'm new in mvc5

I want to display error message for if the customer mobile number filed that not available in database

I tried similarly with this it works well for first condition but with for else doesn't

  $(document).ready(function () {
    $('#customer_mobile').change(function () {
        $.ajax({
            url: '/Cases/History_Test?searchMobile=' + $('#customer_mobile').val(),
            method: 'GET',
            success: function (data) {

                if('#customer_mobile' == true)
                {
                    var Output = "<h4 align=center> History of Cases for</h4>";
                    Output += "<table>"
                    Output += "<tr>"
                    Output += "<th>" + " Case ID &nbsp;&nbsp; &nbsp;  </th>";
                    Output += "<th>" + " Case Name &nbsp;&nbsp; &nbsp;  </th>";
                    Output += "<th>" + " Customer Name &nbsp; &nbsp; &nbsp;  </th>";
                    Output += "</tr>";
                    for (var i = 0; i < data.length; i++)
                    {
                        Output += "<tr>"
                        Output += "<td>" + "&nbsp" + data[i].case_id + "&nbsp; </td>";
                        Output += "<td>" + data[i].case_name + "&nbsp; &nbsp; &nbsp; &nbsp;  </td>";
                        Output += "<td>" + data[i].customer_name + "&nbsp; </td>";
                        Output += "</tr>"
                    }

                    Output += "</table>"

                    //alert(Output);
                    $('#partView').html(Output);
                }
                else{
                    $('#customer_mobile').change(function () {
                        $.ajax({
                            url: '/Cases/Check_Customer?searchMobile=' + $('#customer_mobile').val(),
                            method: 'GET',
                            success: function (data) {
                                alert("Error this customer not avalibale ")
                            }
                            });
                    });
                }
            }
        });
    });
});

and my controller

    public ActionResult History_Test(string searchMobile)
    {
        var cases = from c in db.Case
                    select c;
        if (!String.IsNullOrEmpty(searchMobile))
            cases = cases.Where(c => c.Customer.customer_mobile.Contains(searchMobile));
        else
            cases = cases.Where(p => p.case_id == -1);
        return Json(cases.Select(d => new { case_id = d.case_id, case_name = d.case_name, customer_mobile = d.customer_mobile, customer_name = d.Customer.customer_name }), JsonRequestBehavior.AllowGet);
    }


    public ActionResult Check_Customer(string CheckhMobile)
    {
        var cases = from c in db.Case
                    where c.customer_mobile==CheckhMobile
                    select c;

        if (cases.Count() > 0)
            return Json(new { exising  = true }, JsonRequestBehavior.AllowGet);
        else
            return Json(new { exising = false }, JsonRequestBehavior.AllowGet);
    }

Aucun commentaire:

Enregistrer un commentaire