0

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();

....

}
5
  • where is selectedDate being initialized? Commented Feb 14, 2014 at 2:27
  • further up in the code. I am setting it from another value in control and it works fine. Commented Feb 14, 2014 at 2:28
  • Perhaps unrelated, but in your success function, you name your input parameter result but then refer to it as data. Commented Feb 14, 2014 at 2:30
  • check the headers your developer tools - are they all getting passed to your server correctly? Commented Feb 14, 2014 at 2:30
  • thanks yes unrelated, i copied the if statement from other code and didn't change it, thanks for the catch Commented Feb 14, 2014 at 2:31

1 Answer 1

1

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
};
Sign up to request clarification or add additional context in comments.

4 Comments

I need all the properties on the object to passed back to me.
Isn't selectedTime the object with all your properties?
actually - if you're doing conversion into an object you are probably correct
Thanks!!!! I renamed the setTime to newAppt in the receiving method signature and it worked perfectly. Didn't even think the names were causing issues.

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.