ASP.NET MVC 通过 ajax 将数据传递到部分视图



我正在使用完整的日历库,我将事件加载为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; }

}
}

编辑

这仍然导致id在添加内容方法空

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,那么它应该可以工作。

相关内容

  • 没有找到相关文章

最新更新