ChampChris
ChampChris

Reputation: 1631

Data object in Ajax

I have this code inside a function:

 var setTime = selected[0];
 var selectedTime = {
     ProfessionalUserId: $("#SelectProId").text()
     , Date: selectedDate
     , SetTime: setTime
     , Time: null
 };

$.ajax({
    type: 'POST',
    data: selectedTime,
    url: '@Url.Action("SetAvailableAppointments", "ScheduleView")',
    dataType: "json",
    //contentType: 'application/json',
    success: function (result) {
            if (data === true) {
               $("#successfulAppt").show();
             }
    },
    error: function (err) {

   }

})

The selected[0] set a string value that represets a time, i.e. "11:00:00" or "10:15:00, etc...

the selectedTime object matches a server side POCO object which is in the signature of the receiving method.

The problem is that if i create the selectedTime object like this with the SetTime property:

var setTime = selected[0];
var selectedTime = {
    ProfessionalUserId: $("#SelectProId").text()
    , Date: selectedDate
    , SetTime: setTime
    , Time: null
};

the receiving method receives a null object.

if i create the same object selectedTime this way:

var setTime = selected[0];
var selectedTime = {
     ProfessionalUserId: $("#SelectProId").text()
     , Date: selectedDate
     //, SetTime: setTime
     , Time: null
};

without the SetTime property the receiving method accepts the object.

SetTime is being set to a string value. I have proved this step through the code with chrome develper tools.

Here is the server side object

public class AvailableTime
    {
        public DateTime Date
        {
            get;
            set;

        }
        public TimeSpan Time { get; set; }
        public string TimeLiteral
        {
            get
            {
                return Time.ToString();
            }
        }

        public string SetTime { get; set; }
        public int ProfessionalUserId { get; set; }
    }

here is the receiving method:

public ActionResult SetAvailableAppointments(AvailableTime setTime)
        {
            if (setTime == null)
                return View();

....

}

Upvotes: 0

Views: 61

Answers (1)

John Fisher
John Fisher

Reputation: 22719

It looks like you need to change your ajax call to set your parameter this way:

data: { setTime: selectedTime },

That works because your function parameter is named setTime, and you created an object called selectedTime like this:

var selectedTime = {
    ProfessionalUserId: $("#SelectProId").text()
    , Date: selectedDate
    , SetTime: setTime
    , Time: null
};

Upvotes: 1

Related Questions