我正在将jquery fullcalendar集成到我的应用程序中。 这是我正在使用的代码:
index.aspx中的:
<script type="text/javascript">
$(document).ready(function() {
$('#calendar').fullCalendar({
events: "/Scheduler/CalendarData"
});
});
</script>
<div id="calendar">
</div>
以下是Scheduler / CalendarData的代码:
public ActionResult CalendarData()
{
IList<CalendarDTO> tasksList = new List<CalendarDTO>();
tasksList.Add(new CalendarDTO
{
id = 1,
title = "Google search",
start = ToUnixTimespan(DateTime.Now),
end = ToUnixTimespan(DateTime.Now.AddHours(4)),
url = "www.google.com"
});
tasksList.Add(new CalendarDTO
{
id = 1,
title = "Bing search",
start = ToUnixTimespan(DateTime.Now.AddDays(1)),
end = ToUnixTimespan(DateTime.Now.AddDays(1).AddHours(4)),
url = "www.bing.com"
});
return Json(tasksList,JsonRequestBehavior.AllowGet);
}
private long ToUnixTimespan(DateTime date)
{
TimeSpan tspan = date.ToUniversalTime().Subtract(
new DateTime(1970, 1, 1, 0, 0, 0));
return (long)Math.Truncate(tspan.TotalSeconds);
}
public ActionResult Index()
{
return View("Index");
}
我在site.master中的head标记内也有以下代码:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" />
<link href="<%= Url.Content("~/Content/jquery-ui-1.7.2.custom.css") %>" rel="stylesheet" type="text/css" />
<link href="~Perspectiva/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~Perspectiva/Content/fullcalendar.css" rel="stylesheet" type="text/css" />
<script src="~Perspectiva/Scripts/jquery-1.4.2.js" type="text/javascript"></script>
<script src="~Perspectiva/Scripts/fullcalendar.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
我所做的一切都是从http://szahariev.blogspot.com/2009/08/jquery-fullcalendar-and-aspnet-mvc.html
复制的当导航到/ scheduler / calendardata时,我得到一个保存json数据的提示,其内容正是我在CalendarData函数中创建的内容。
为了正确呈现页面,我需要做些什么?
提前致谢,
叶兰
更新:根据Rune和Franci的评论,我添加了一个名为CalendarData.aspx的视图,它与index.aspx相同。结果:
答案 0 :(得分:1)
尝试使用JSonResult而不是ActionResult。
public JSonResult CalendarData()
{
IList<CalendarDTO> tasksList = new List<CalendarDTO>();
tasksList.Add(new CalendarDTO
{
id = 1,
title = "Google search",
start = ToUnixTimespan(DateTime.Now),
end = ToUnixTimespan(DateTime.Now.AddHours(4)),
url = "www.google.com"
});
tasksList.Add(new CalendarDTO
{
id = 1,
title = "Bing search",
start = ToUnixTimespan(DateTime.Now.AddDays(1)),
end = ToUnixTimespan(DateTime.Now.AddDays(1).AddHours(4)),
url = "www.bing.com"
});
return Json(tasksList, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
您需要导航到映射到返回Index.aspx的操作的URL,而不是映射到CalendarData()的URL。我认为这将是“/ scheduler”之类的东西。当你转到“/ scheduler / calenderData”时,服务器会将Json数据直接返回给浏览器。
答案 2 :(得分:0)
如果您在浏览器中导航到/scheduler/calendarData
,则浏览器会收到Content-Type: application/json
的回复。有些浏览器不知道如何显示这样的内容类型,因此它们允许您将内容保存为本地文件,因此您可以在其他程序中查看它。
您应该导航到浏览器/scheduler/index
,这会返回一个人类可读的结果,可以由所有浏览器呈现(Content-Type: text/html
}。
如果您想查看CalendarData
返回的内容以进行调试,您有两种选择:
Content-Type: application/json
答案 3 :(得分:0)
尝试使用JSonResult而不是ActionResult。
答案 4 :(得分:0)
我有同样的问题,我错过了基本的ActionResult,它返回View以开始...?我认为这是更深层次的东西......但这里有一些基本的东西可以检查我的问题;
在我的家庭控制器中;
public ActionResult Index()
{
ViewData["Message"] = "Whatever!!! just work!";
return View();
}
在Global中,我还不得不将我的maproutes重新修改为默认值,因为我在尝试修复它时正在转向GetData操作:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
答案 5 :(得分:0)