I have below code in Controller
// POST api/PersonalDetails
[ResponseType(typeof(PersonalDetails))]
[HttpPost]
public IHttpActionResult PostPersonalDetails(PersonalDetails personaldetails)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.PersonalDetails.Add(personaldetails);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = personaldetails.AutoId }, personaldetails);
}
and below code in view
<script type="text/javascript">
$("#btnAdd").click(function () {
var PersonalDetails = {
"FirstName": $("#FirstName").val(),
"LastName": $("#LastName").val(),
"Age": $("#Age").val(),
"Active": $("#Active").val()
};
$.ajax({
type: "POST",
url: 'http://localhost:28206/api/PersonalDetails/PostPersonalDetails',
data: JSON.stringify(PersonalDetails),
dataType: "json",
processData: true,
success: function (data, status, xhr) {
alert(status);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
});
</script>
Now, when I am clicking on the button that fires up btnAdd click method. The controller PostPersonalDetails method is executing but personaldetails object is null. I am not able to retrieve values of the form.
How to get values of the PersonalDetails object formed in the click event.