0

When Debugging the MVCcontroller, all the properties of eventDetails are null, although from the client side the properties have values

Ajax Call

  $.ajax({
            type: "POST",
            url: "/Home/schedule",
            data: {
                EventDetailsId: 1,
                Location: scheduleData.location,
                EventName: scheduleData.title

            },
            contentType: 'application/json',
            success: function (data) {
                alert(data);
            },
            error: function () {
                alert("Error occured!!")
            }
        });

EventDetails Class

    public class EventDetails
        {
            public int EventDetailsId{ get; set; }
            public string Location { get; set; }
            public string EventName { get; set; }
    }

Post Call

 [HttpPost]
    public JsonResult Schedule(EventDetails eventDetails)
    {
        return Json(new { status = true });
    }

enter image description here

enter image description here

2 Answers 2

1

When your input is complex type,Then server expects to receive a json format and deserialize it to the object type defined,in this scenario you are gonna have to meet server expectation. Change your javascript code as follows:

var arr = {
    'EventDetailsId': 1,
    'Location': scheduleData.location,
    'EventName': scheduleData.title

};
$.ajax({
    type: "POST",
    url: "/Home/schedule",
    data: JSON.stringify(arr),
    contentType: 'application/json',
    success: function (data) {
        alert(data);
    },
    error: function () {
        alert("Error occured!!")
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

I had it that way initially, it was null
are you sure schedule method is called? did you check it in debug mode? did you hit F12 in your browser to see any possible errors?
yes, there are no errors, when it hits the schedule method the parameter values are null.
i tested and it worked perfectly ! exact copy of your code !
@Curious-programmer Did you happen to miss the JSON.stringify() when you tried it yourself? That may be what you are missing.
0

I added [FromBody] attribute and the values are not null.

 [HttpPost]
    public JsonResult Schedule( [FromBody]EventDetails eventDetails)
    {
        return Json(new { status = true });
    }

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.