现在在我的ASP.NET MVC Core2项目中,我在EF database
中有一个模型,其中包含几个属性:
public class SchoolEvents
{
public long ID { get; set; }
[Required]
[StringLength(40, ErrorMessage = "Max 40 characters")]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
public DateTime WhenHappens { get; set; }
}
我没有问题,可以通过MVC EF database
从Razor Views
获取数据。但是我在其中一个视图中使用了JavaScript
日历插件,它将在其上标记来自db
的事件。为此,脚本正在采用以下格式的数据:
{ title: 'EventTitle', description: 'Few words about the event', datetime: new Date(2018, 8, 14, 16) }
很明显,我应该在脚本中使用for
循环,对db
对象进行迭代。
由于我对JS
仍然不满意,所以目前我唯一知道的方法是:
-在控制器中创建JSON文件:
[Route("events")]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public ActionResult Comments()
{
var _events= _context.Events.OrderBy(c => c.ProductID).ToList(); //yes, I know, I should use repository in the best practice
return Json(_events);
}
-在JS
文件中,我可以使用kinf of loadEventsFromServer()
函数,该函数使用XMLHttpRequest
或Fetch
进行解析(我尚不知道如何进行解析) ,我很乐意得到一些建议),
就是这样。您还有其他想法怎么做吗?
编辑:
使用部分插件代码进行更新,以获取控制台错误d is undefined
:
for (var i = 0; i < 42; i++) {
var cDay = $('<div/>');
if (i < dWeekDayOfMonthStart) {
cDay.addClass('c-day-previous-month c-pad-top');
cDay.html(dLastDayOfPreviousMonth++);
} else if (day <= dLastDayOfMonth) {
cDay.addClass('c-day c-pad-top');
if (day == dDay && adMonth == dMonth && adYear == dYear) {
cDay.addClass('c-today');
}
for (var j = 0; j < settings.events.length; j++) {
var d = settings.events[j].datetime;
if (d.getDate() == day && d.getMonth() == dMonth && d.getFullYear() == dYear) {
cDay.addClass('c-event').attr('data-event-day', d.getDate());
cDay.on('mouseover', mouseOverEvent).on('mouseleave', mouseLeaveEvent);
}
}
cDay.html(day++);
} else {
cDay.addClass('c-day-next-month c-pad-top');
cDay.html(dayOfNextMonth++);
}
cBody.append(cDay);
}
答案 0 :(得分:2)
我建议您使用ajax请求。
Javascript:Ajax
[Route("events")]
[HttpPost]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
public ActionResult Comments()
{
var _events= _context.Events.OrderBy(c => c.ProductID).ToList(); //yes, I know, I should use repository in the best practice
return Json(_events);
}
控制器
helm get values <namespace> -a