0

I am doing a Tutoring form where a tutor selects from a checkbox a day of the week and from 3 dropdowns a time from and a time until, am/pm time that he can teach at that specific day. I need to save all the selected days and times in the database.

So what I have is this:

[checkbox]Sunday [dropdown]4 - [dropdown]5 [dropdown]am/pm <br />
[checkbox]Monday [dropdown]5 - [dropdown]6 [dropdown] am/pm <br />

and so on...

I am using a hash that uses the day as the key and the times as values like this:

var TutorDays = new Object();


Then I loop through the form to save the values for each key as follows:

var Day; $("input[type=checkbox]").each(function () { Day = $('label[for=' + this.id + ']').html();

        TutorDays[Day] = "{" + Day + ":" + $("#ddlTimeFrom" + Day).val() + ":" +   $("#ddlTimeTo" + Day).val() + ":" + $("#ddlAmPm" + Day).val() + "}";
    });

So the saved value in the key is like this: {Sunday:1:2:pm} Then I use ajax post and here is where I am stuck. I don't know how to pass the whole hash and then parse it in c# code's we bmethod. If I just put TutorDays["Sunday"] then It send the value of TutorDays["Sunday"] as expected. Is there an elegant and efficient way to pass the whole collection to the WebMethod and then parse the keys and values in C# code.
Thank you for taking the time to read this.

$.ajax({
            type: "POST",
            url: 'AddEditItem.aspx/SaveTutorData',

data: "{AccountID:'" + AccountID + "'" + ",SubjectID:'" + SubjectID + "'" +       ",SchoolID:'" + SchoolID + "',HourlyRate:'" + HourlyRate + "'" + ",TutorDays:'" +         TutorDays["Sunday"] + "'}",

            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                alert("Saved.");                   
            },
            error: function () {
                alert("Unexpected error occured");
            }
        });
santosh singh
  • 27,666
  • 26
  • 83
  • 129
Albert
  • 41
  • 5
  • `JSON.stringify` the data. But wait...it seems you are using aspx page methods as your webmethod. So how do you propose to work with the data inside the webmethod? – deostroll Dec 25 '13 at 11:17
  • A Dictionary can be used in the .cs file the day as the key and the time as the value. – Albert Dec 25 '13 at 18:19
  • Have a look at this post -> http://stackoverflow.com/questions/5873605/converting-object-variable-to-json-string-for-asp-net-page-method .Its not a direct solution, but enough to give you an idea. Please note you've to set the `contentType` and the `data` parameters of the `$.ajax` call appropriately. If this still doesn't work, you'd have to share the code for the aspx page method. – deostroll Dec 26 '13 at 09:38

0 Answers0