0

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.

1 Answer 1

3

Okay, I solved this problem and have written an article to help others.

Inserting record into database using ASP.NET MVC with Web API

Hope this will be helpful for others looking for similar kind of problem.

Thanks

Sign up to request clarification or add additional context in comments.

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.