-1

I know that the title is somewhat complicated to understand. But problem is like that only. Let I explain you in detail,

I am doing if condition in ajax success, now in am giving condition that if the data is not equal to undefined then that data want to exist.

But I am getting problem is that if data is not equal to undefined still data going under the if condition.

Let see code for more clear

Here is the view

<input type="text" id="Material_PartNo@(i)" data-provide="typeahead" class="typeahead search-query form-control autocomplete" placeholder="PartNo"
onblur="ShowPartNoData(this.value, @(i))" />

<input type="text" class="form-control typeahead search-query autocomplete" id="Material_Description@(i)" data-provide="typeahead" 
placeholder="Enter The Product" onblur="ShowProductData(this.value, @(i), Material_PartNo@(i).value)" />

function ShowProductData(Product, id, partno) {        
    $.ajax({
        url: "/ServiceJob/ShowProductData",
        method: "POST",
        data: { 'product': Product, 'partno': partno },
        dataType: "json",
        success: function (data) {
            if (data.data.name != "undefined");
            {
                alert(data.data.pid != "undefined");
                document.getElementById("Material_PartID" + id).value = data.data.pid,
                document.getElementById("Material_PartNo" + id).value = data.data.name,                    
                document.getElementById("Material_Amount" + id).value = data.data.sellprice,
                document.getElementById("Material_Qty" + id).value = 1,
                document.getElementById("hdn_Material_Amount" + id).value = data.data.sellprice                 
            }
        }
    });
}

Now, here is controller code

[HttpPost]
public IActionResult ShowProductData(string product, string partno)
{
    var productID = _Db.Category.Where(x => x.Name == product).FirstOrDefault();
    if (productID != null)
    {
        if (partno == null)
        {
            var productDataResult = (from fm in _Db.Purchase
                          join s in _Db.Part on fm.PartId equals s.Id
                          where fm.CategoryId == productID.Id //&& s.Name == partno
                          select new
                          {
                              pid = s.Id,
                              sellprice = fm.sellprice,
                              Name = s.Name
                          }).FirstOrDefault();
            return Json(new { data = productDataResult });
        }
        else
        {
            var productDataResult = (from fm in _Db.Purchase
                          join s in _Db.Part on fm.PartId equals s.Id
                          where fm.CategoryId == productID.Id && s.Name == partno
                          select new
                          {
                              pid = s.Id,
                              sellprice = fm.sellprice,
                              Name = s.Name
                          }).FirstOrDefault();
            return Json(new { data = productDataResult });
        }
    }
    else
    {
        return Json(new { data = false });
    }
}

now as per the code, there is two textbox named Material_PartNo@(i) and Material_Description@(i). Whatever I write in Material_Description@(i), there related product will show in another textbox named Material_PartNo@(i). And if not available any product the Material_PartNo@(i) should be blank.

Now for that I use if condition in ajax that if data.data.name is undefined (data.data.name is the value store of Material_PartNo@(i)) then Material_PartNo@(i) should be blank else show their value.

But I see that data.data.name is undefined (by javascript alert) still showing the undefined in it Material_PartNo@(i) textbox instead of blank.

karan
  • 482
  • 1
  • 8
  • 30

1 Answers1

1

According to Detecting an undefined object property, your undefined object property check seem to be wrong because you're checking against string "undefined" with value inequality operator (!=), not checking undefined as type for uninitialized objects.

Try checking undefined state with typeof and type inequality operator (!==) like this example:

$.ajax({
    url: "/ServiceJob/ShowProductData",
    method: "POST", // or use type: 'POST'
    data: { 'product': Product, 'partno': partno },
    dataType: "json",
    success: function (data) {
        if (typeof data.data.name !== "undefined")
        {
            alert(typeof data.data.pid !== "undefined");
            $('#Material_PartID' + id).val(data.data.pid);
            $('#Material_PartNo' + id).val(data.data.name);

            // other stuff                 
        }
        else
        {
            // set empty textboxes here
            $('#Material_PartID' + id).val('');
            $('#Material_PartNo' + id).val('');
        }
    }
});

Note:

Your current if condition has additional semicolon in if (data.data.name != "undefined");, which may become another reason why that if condition not working as intended.

Reference: undefined type

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61