tishantha
tishantha

Reputation: 497

Ajax Json calling MVC4 Controller Method Parameter Always Null

I am using Ajax with MVC4 web application. I've got a problem with passing values to action method. It's always pass the null as the parrameter value. Here is my codes.

    function onChange(arg) {
    var adrId = $.map(this.select(), function (item)
    {
        return $(item).find('td').first().text();
    });

      GetEntries(adrId);//Calling the function

    }

function GetEntries(adrId) {

    //alert("AdrId >> "+adrId); here it shows value is 3465

    $.ajax({
        url: 'Customer/LoadCustomer',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ adrId: adrId }),
        success: function (result) {
            alert("success");
        }
    });
} 


    [HttpPost]
    public JsonResult LoadCustomer(string adrId)//HERE value is ALLWAYS NULL.. :(
    {
        Address_SelectW adrList = new Address_SelectW();
        adrList = this.commonObj.CustomersSelectByAdrKy(cky, usrKy, AdrKy);
        return Json(adrList, JsonRequestBehavior.AllowGet);
    }

Please help me to solve this problem. Thank you.. :)

=========================================================================== Additional Informations....

I have used another one to insert data. it's worked fine..

 $("#btnSave").click(function () {

    //var ContactID = $("#txtContactId").val();
    var Company = $("#txtCompany").val();
    var Status = $("#cmbStatus").val();
    var IsActive = $("#IsActive").is(':checked');
    var Comments = $("#txaComments").val();
    var Country = $("#cmbCountry").val();
    var Address1 = $("#txtAddress1").val();
    //var Address2 = $("#txtAddress2").val();
    var City = $("#txtCity").val();
    var State = $("#txtState").val();
    var PostalCode = $("#txtPostalCode").val();
    var VatNo = $("#txtVatNo").val();
    var RegNo = $("#txtRegNo").val();
    var Phone = $("#txtPhone").val();
    var Email = $("#txtEmail").val();
    var AdrKey = $("#AdrID").val();


    $.ajax({
        url: "Customer/InsertCustomer",
        data: {
            //'ContactID': ContactID,
            'Company': Company,
            'Status': Status,
            'IsActive': IsActive,
            'Comments': Comments,
            'Country': Country,
            'Address1': Address1,
            //'Address2': Address2,
            'City': City,
            'State': State,
            'PostalCode': PostalCode,
            'VatNo': VatNo,
            'RegNo': RegNo,
            'Phone': Phone,
            'Email': Email
        },
        dataType: "json",
        type: 'POST',
        success: function (data) {
            alert("Successfully Inserted!");
        },
        error: function () {
            alert("error");
        }
    });


});



    [HttpPost]
    public ActionResult InsertCustomer(string Company, int Status, bool IsActive, string Comments, int Country, string Address1, string City, string State, string PostalCode, string VatNo, string RegNo, string Phone, string Email)
    {
        AdrCustomModel model = new AdrCustomModel();
        bool process = false;

        model.Company = Company;
        model.Status = Status;
        model.IsActive = IsActive;
        model.Comments = Comments;
        model.Country = Country;
        model.Address1 = Address1;
        model.City = City;
        model.State = State;
        model.PostalCode = PostalCode;
        model.VatNo = VatNo;
        model.Phone = Phone;
        model.RegNo = RegNo;
        model.Email = Email;
        model.cky = cky;

        model.ContactID = this.CustomerID(Status);

        process = this.commonObj.InsertAdrCustomer(model,usrKy);

        Accounts_Select accmodel = new Accounts_Select();
        accmodel.CKy = cky;
        accmodel.AccCd = model.ContactID;
        accmodel.AccNm = Company;
        accmodel.AccTypKy = this.commonObj.AccTypeKyByPrefixKy(Status);

        process = this.commonObj.InsertAccount(accmodel, usrKy);

        return Json(process, JsonRequestBehavior.AllowGet);
    }

I have no idea about why this one is working fine and that one is not working. I have tried both JsonResult and ActionResult to Action method. And also tried with and without [HttpPost]. But always Parrameter value is NULL

Upvotes: 0

Views: 8032

Answers (4)

tishantha
tishantha

Reputation: 497

It required concatenate "" with the parameter which you wanna pass to Action method.

    function GetEntries(adrId) {

 var NewAdrId = ""+adrId; //<<<<<<<<<<<< Answer<<<<<<<<<<<<<<


$.ajax({
    url: 'Customer/LoadCustomer',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ adrId: NewAdrId }),
    success: function (result) {
        alert("success");
    }
});

}

// Thanks :)

Upvotes: 0

hungry
hungry

Reputation: 37

I'm not sure if it will solve your problem but you can try this.

Place [WebMethod] attribute in your controller method.

or you can you can pass url with appended id like

'Customer/LoadCustomer'+ adrId

Upvotes: 1

Jens Neubauer
Jens Neubauer

Reputation: 1090

In your first example, you send a JSON object, in the second you just post data.

JSON is unnecessarily complicated to send just one value. Try this instead:

$.ajax({
    url: 'Customer/LoadCustomer',
    type: 'POST',        
    data: {'adrId': adrId },
    dataType: 'json',
    success: function (result) {
        alert("success");
    }
});

Upvotes: 0

Stephen
Stephen

Reputation: 2047

Put the property name in quotes:

data: JSON.stringify({ 'adrId': adrId }),

Upvotes: 0

Related Questions