我正在使用完整的日历库,我将事件作为JSON加载并在日历中显示。 接下来我要做的是加载最新事件的id并在局部视图中显示模型中有关事件的信息。到目前为止,这是我的代码:
<div id="calendar"></div>
<div id="myPartial"></div>
<script>
$('#calendar').fullCalendar({
aspectRatio: 3,
events: function (start, end, timezone, callback) {
$.getJSON("@Url.Action("GetEvents")", function (locationsArray) {
var result = $(locationsArray).map(function () {
return {
id: this.idEvent,
title: this.eventName,
start: this.startTime,
end: this.endTime,
allDay: this.allDay
};
}).toArray();
callback(result);
var id_latest = result[result.length - 1].id
success: $(function (id_latest) {
$.ajax({
type: "POST",
url: '@Url.Action("AddContent","Home")',
data: { id: id_latest },
error: function () {
alert("An error occurred.");
},
success: function (data) {
$("#myPartial").html(data);
}
});
});
});
}
});
</script>
我在这里缺少什么?这样做的正确方法是什么?
编辑:
控制器:
namespace bezeckaApp.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult AddContent(int? id)
{
return PartialView("~/Views/Home/AddContent.cshtml");
}
public ActionResult GetEvents()
{
List<Treninky> trenink = new List<Treninky>();
trenink.Add(new Treninky() { idEvent = 1, eventName = "Morning run", startTime = "2017-07-01T08:00:00Z", endTime = "2017-07-01T09:00:00", allDay = false });
trenink.Add(new Treninky() { idEvent = 2, eventName = "Evening run", startTime = "2017-07-05T17:00:00Z", endTime = "2017-07-05T18:00:00", allDay = false });
trenink.Add(new Treninky() { idEvent = 3, eventName = "Evening run", startTime = "2017-07-08T17:00:00Z", endTime = "2017-07-08T18:00:00", allDay = false });
return Json(trenink, JsonRequestBehavior.AllowGet);
}
}
}
型号:
namespace bezeckaApp.Models
{
public class Treninky
{
public int idEvent { set; get; }
public string eventName { set; get; }
public string startTime { set; get; }
public string endTime { set; get; }
public bool allDay { set; get; }
}
}
修改
这仍然会导致AddContent方法中的 id NULL
答案 0 :(得分:0)
public ActionResult AddContent(int? id)
{
List<Treninky> trenink = new List<Treninky>();
trenink.Add(new Treninky() { idEvent = 1, eventName = "Morning run", startTime = "2017-07-01T08:00:00Z", endTime = "2017-07-01T09:00:00", allDay = false });
trenink.Add(new Treninky() { idEvent = 2, eventName = "Evening run", startTime = "2017-07-05T17:00:00Z", endTime = "2017-07-05T18:00:00", allDay = false });
trenink.Add(new Treninky() { idEvent = 3, eventName = "Evening run", startTime = "2017-07-08T17:00:00Z", endTime = "2017-07-08T18:00:00", allDay = false });
return PartialView("~/Views/Home/AddContent.cshtml",trenink.Find(id));
}
如果您的javascript正在运行,并且您获得了最新的ID,那么它应该有效。