0

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

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

It also doesn't work. but when I put ""+adrId it worked. It required concatenate with "". Thank you.. :)
0

Put the property name in quotes:

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

1 Comment

data: JSON.stringify({ 'adrId': adrId }), Doesn't worked. But when I put ""+adrId it worked. It required concatenate with "". thank you! :)
0

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

1 Comment

When I put ""+adrId it worked. It required concatenate with "". Thank you.. :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.