[英]What is the best and most efficient way to get data by JavaScript from EntityFramework database in my case?
Right now in my ASP.NET MVC Core2 project I have a model in EF database
, that contains several properties: 现在,在我的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; }
}
I have no problem to get data from the EF database
by MVC Razor Views
. 我没有问题,可以通过MVC
Razor Views
从EF database
获取数据。 But I am using JavaScript
Calendar plugin in one of my Views, that will mark events from db
on it. 但是我在其中一个视图中使用了
JavaScript
Calendar插件,它将标记db
上的事件。 To do it, the script is taking data in format: 为此,脚本以以下格式获取数据:
{ title: 'EventTitle', description: 'Few words about the event', datetime: new Date(2018, 8, 14, 16) }
It seems to be obvious, that I supposed to use a for
loop in the script, iterating on db
objects. 似乎很明显,我应该在脚本中使用
for
循环,对db
对象进行迭代。
As I am still noob about JS
, right now the only way I know to do it is: 由于我对
JS
仍然不了解,现在我知道的唯一方法是:
-to create JSON file in the controller: -在控制器中创建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);
}
-in JS
file I can use kinf of loadEventsFromServer()
function, that uses XMLHttpRequest
or Fetch
and parsing it (I do not know yet how to do the parsing, I will be happy to get some suggestions), -在
JS
文件中,我可以使用loadEventsFromServer loadEventsFromServer()
函数的loadEventsFromServer()
,该函数使用XMLHttpRequest
或Fetch
进行解析(我尚不知道如何进行解析,我很乐意得到一些建议),
And that it is it. 就是这样。 Do you have some other ideas how to do it?
您还有其他想法怎么做吗?
EDIT : 编辑 :
Update with part of plugins code, for console error d is undefined
: 使用部分插件代码进行更新,因为控制台错误
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);
}
I will suggest you to use ajax request. 我建议您使用ajax请求。
Javascript : Ajax
Javascript:Ajax
$.ajax({
type: 'POST',
url: '@URL.Action("Comments","Controller")',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: {},
success: function (data) {
var events = new Object();
events = $.map(data.d, function (item, i) {
for (var j = 0; j < data.d.length; j++) {
var event = new Object();
var startDate = Date.parse(item.WhenHappens )
event.start = startDate;
event.title = item.Title;
event.backgroundColor = "#c6458c";
event.description = item.Description;
return event;
}
})
callCalender(events);
},
error:function(e){
}
});
Controller
调节器
[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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.