0

My text box and button

<input type="text" class="form-control" name="ClaimNumber" placeholder="Enter a claim number"
                id="ClaimNumber" />               
<button class="btn btnNormal" type="submit" id="btnSearch">
        <i class="fa fa-search"></i>
</button>

My jquery

$(document).ready(function () {
    $("#btnSearch").on("click", function () {
            var enteredClaimNumber= $("#ClaimNumber").val();
            alert(enteredClaimNumber);
             $.ajax({
            type: "POST",
            url: "/Home/ClaimsSearch",
            data: enteredClaimNumber
        });
    });
});

My controller

    [HttpPost]
    public ActionResult ClaimsSearch(string enteredClaimNumber)
    {
        _lfAPI.ClaimsAdvanceSearch(enteredClaimNumber);
        return View();
    }

I'm not able to get the value in controller..Thanks in advance..

4
  • 2
    data: {enteredClaimNumber: ClaimNumber}? Commented May 4, 2015 at 12:41
  • data : "&enteredClaimNumber="+ClaimNumber, try this it will work Commented May 4, 2015 at 12:44
  • The control is coming to controller, but value is shown as null... Commented May 4, 2015 at 12:44
  • As you have edited the post, use data: {enteredClaimNumber: enteredClaimNumber} The name : value, name should match controller method param name; and the value should match the javascript variable/value you are passing. Commented May 4, 2015 at 12:46

3 Answers 3

1

Data in AJAX request must be like name, value pair:

data: {"enteredClaimNumber": ClaimNumber}

Write like this:

$(document).ready(function () {
    $("#btnSearch").on("click", function () {
            var ClaimNumber = $("#ClaimNumber").val();
            alert(enteredClaimNumber);
             $.ajax({
            type: "POST",
            url: "/Home/ClaimsSearch",
            data: {"enteredClaimNumber": ClaimNumber}          
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

The property name must match the action parameter enteredClaimNumber.
1
var datum = {"claimNum": ClaimNumber};  
$.ajax ({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     url: "/Home/ClaimsSearch",
     dataType: "json",
     data: JSON.stringify(datum),
});

Comments

0

![Check This][1]

In
data : { parameter : value }
if there are multiple parameter then separate with comma (,)
data : { parameter1 : value1,parameter2 : value2 }
as shown below
$.ajax({
url: this.href,
type: 'POST',
data: { input: $('#caption').val() },
success: function (result) {
alert(result.name);
},
error: function () {
alert("error");
}
});

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.