Fullcalendar:将事件作为C#字符串传递

时间:2015-11-18 13:46:55

标签: c# json string fullcalendar

我有关于fullcalendar插件的以下问题:是否可以将事件作为字符串变量传递?我道歉,我是新手,我已经尝试了json和xml但是无法让那些工作(是的,那是我是失败者)而且我决定尝试丑陋但是如果它的工作方式。但是,它也不起作用,我想问是否有可能。

当我需要从数据库中提取数据时,有没有人可以解释我如何处理这种情况,将其解析为json并将其作为事件提供给fullcalendar(在我的代码中:机器)?我已经从数据库中读取数据并将其放入列表中,也可以将其序列化为json,但是如何将其提供给事件?需要什么功能?

我的代码在这里:

在aspx文件中没有与日历相关的内容,但在代码背后:

public string WorstCodeEverMakeEvent(string ttl, DateTime startdate, DateTime enddate, string starttime)
            { //this function make string of one event
                string datestart = "new Date(" + startdate.Year + ", " +
            startdate.Month + ", " + startdate.Day + ", " + Convert.ToDateTime(starttime).Hour + ", " +
            Convert.ToDateTime(starttime).Minute + ", " + Convert.ToDateTime(starttime).Second + ")";
        string ohnowhy = "{title: " + ttl + ", start: " + datestart + "}";
        return ohnowhy;
    }
    protected void btnCheckTimetable_Click(object sender, EventArgs e)
    {               // date, machine_id, time, length, suborder_id, order_id
         List<Tuple<string,int,string,int,int,int>> machines = new List<Tuple<string,int,string,int,int,int>>();
        DateTime endDate = DateTime.Now.AddDays(22);
        for (DateTime dt = DateTime.Now.AddDays(-2); dt <= endDate; dt = dt.AddDays(1))
        {
            SqlCommand getMachineTimes = new SqlCommand
               (@"SELECT TimeStart, Length, SubOrderID, OrderID
                   FROM MachineDates 
                    WHERE MachineID=@mid AND [Date]=@dt", con);
            getMachineTimes.Parameters.AddWithValue("@mid", lbxMachines.SelectedValue);
            getMachineTimes.Parameters.AddWithValue("@dt", dt.ToShortDateString());
            if (con.State == ConnectionState.Closed)
                con.Open();
            reader = getMachineTimes.ExecuteReader(); // this is to get data    about machines from database
    // machines = events
            while (reader.Read())
            {
                if (!reader.IsDBNull(0))
                    machines.Add(new Tuple<string, int, string, int, int, int>
                        (dt.ToShortDateString(), int.Parse(lbxMachines.SelectedValue),
                        reader[0].ToString(), int.Parse(reader[1].ToString()),
                        int.Parse(reader[2].ToString()), int.Parse(reader[3].ToString())));
                else
                    machines.Add(new Tuple<string, int, string, int, int, int>
                        (dt.ToShortDateString(), int.Parse(lbxMachines.SelectedValue),
                        null, 0, 0, 0));
            }
            reader.Close();
        }

        if (machines.Count != 0) //&& users.Count != 0
        {
            string allevents = "events:[";
            foreach (Tuple<string, int, string, int, int, int> tup in machines)
            {
                allevents += WorstCodeEverMakeEvent
                    (tup.Item2.ToString(), Convert.ToDateTime(tup.Item1),
                    Convert.ToDateTime(tup.Item1).AddHours(tup.Item4), tup.Item3);
            }

             ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "script",
    @"<script type='text/javascript'>
    $(document).ready(function () // page is now ready, initialize the   calendar...
    {
     var calendar;
    calendar = $('#calendarMain').fullCalendar(
        {
            weekMode:'#liquid',
            weekends: true,
            firstDay: 1,
            weekNumbers: true,
              theme: true,"
            + allevents+@"],
            //eventLimit: true,
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            //defaultDate: new Date(y, m, d, 9, 00),
            editable: true,
            //defaultView: 'agendaMonth',
        });
})
    </script>", false);

        }

        }

1 个答案:

答案 0 :(得分:0)

似乎让它变得更复杂并不是解决问题的最佳方法。但我唯一看错的是:

string ohnowhy = "{title: " + ttl + ", start: " + datestart + "}";

应该是这样的:

string ohnowhy = "{title: \"" + ttl + "\", start: " + datestart + "}";

编辑:我也不确定你的约会对象。您可以仔细检查the documentation,但我总是以这种格式传递日期:'2014-05-01'