Mahesh Gawhane
Mahesh Gawhane

Reputation: 61

Ajax call not working what is wrong in this code?

This is my view

<td>  
@if (item.Flag == false)
{ 
   <a href="javascript:void(0)" id="@item.CustId" class="flag" onclick="flagCustomer('flase',this.id)">
   <img src="~/Images/appimg/star.png" alt="" /></a>
}
else
{   
    <a href="javascript:void(0)" class="flag" id="@item.CustId" onclick="flagCustomer('true',this.id)">
    <img src="~/Images/appimg/MapMarker_Flag.png" /></a>
}
</td>

This is My Ajax call

 function flagCustomer(flag, id) {
        debugger;
        var flag = flag;
        var id = id;
        debugger;
        $.ajax({
            type: "POST",
            url: "@Url.Action("Importantmark", "Customer")",
            data: { _flag: flag, _id: id },
            success: function (data) {
                if (data === "1") {
                    notif({ msg: "<b>Important Customer.", type: "success" });
                } else {
                    notif({ msg: "<b>error customer while important.", type: "error" });
                }
            },
            Error: function (data) {
                notif({ msg: "<b>error customer while important.", type: "error" });
            }
        });
        debugger;
    }

This is my Controller

[HttpPost]
public int Importantmark(string _flag, int _id)
{
    Customer _customer = new Customer();
    _customer.Flag = Convert.ToString(_flag);
    _customer.CustId = Convert.ToInt32(_id);
    var result = customerBal.ImportantMark(_customer);
    return result;
}

This is my code When I submit click on this image my java script function is call but when I debug code that time it's shown me id and flag property. success debugger came to success property its went outside. what is wrong in this code kindly explain me

Upvotes: 1

Views: 103

Answers (3)

Nic
Nic

Reputation: 12855

Might be your quotes:

url: '@Url.Action("Importantmark", "Customer")',

Also need to add content type, and don't need to reinit flag and id, so would be:

function flagCustomer(flag, id) {
        debugger;
        $.ajax({
            type: "POST",
            url: '@Url.Action("Importantmark", "Customer")',
            data: { _flag: flag, _id: id },
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                if (data === "1") {
                    notif({ msg: "<b>Important Customer.", type: "success" });
                } else {
                    notif({ msg: "<b>error customer while important.", type: "error" });
                }
            },
            Error: function (data) {
                notif({ msg: "<b>error customer while important.", type: "error" });
            }
        });
        debugger;
    }

Upvotes: 0

Vikas Vanvi
Vikas Vanvi

Reputation: 462

Pass contenttype and datatype it should work

function flagCustomer(flag, id) {
        debugger;
        $.ajax({
            method: "POST",
            url: '@Url.Action("Importantmark", "Customer")',
            data: "{ '_flag': '" + flag+ "','_id': '" + id+ "' }",
            contentType: "application/json; charset=utf-8",
            async: true,
            dataType: "json",
            success: function (data) {
                if (data === "1") {
                    notif({ msg: "<b>Important Customer.", type: "success" });
                } else {
                    notif({ msg: "<b>error customer while important.", type: "error" });
                }
            },
            error: function (data) {
                notif({ msg: "<b>error customer while important.", type: "error" });
            }
        });
        debugger;
    }

Upvotes: 3

Bikee
Bikee

Reputation: 1197

First change the following line:

<a href="javascript:void(0)" id="@item.CustId" class="flag" onclick="flagCustomer('flase',this.id)">

To:

<a href="javascript:void(0)" id="@item.CustId" class="flag" onclick="flagCustomer('false',this.id)"> // flase to false

Check if it works:

[HttpPost]
public string Importantmark(string _flag, string _id) //change int to string
{
    Customer _customer = new Customer();
    _customer.Flag = Convert.ToString(_flag);
    _customer.CustId = Convert.ToInt32(_id);
    var result = customerBal.ImportantMark(_customer);
    return result.ToString();
}

Upvotes: 0

Related Questions