简体   繁体   English

在我的情况下,通过JavaScript从EntityFramework数据库中获取数据的最佳和最有效的方法是什么?

[英]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 ViewsEF 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() ,该函数使用XMLHttpRequestFetch进行解析(我尚不知道如何进行解析,我很乐意得到一些建议),

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.

相关问题 对于我的特定情况,将数据从PHP传递到JavaScript的最佳方法 - Best way to pass data from PHP to JavaScript for my particular case Javascript:获得零下限的最有效方法是什么? - Javascript: what is the most efficient way to get a lower bound of zero? 在JavaScript中存储鼠标移动数据的最有效处理方式是什么? - What is the most processing efficient way to store mouse movement data in JavaScript? 从JavaScript中的关联数组中获取第一个项目的最有效方法是什么? - What is the most efficient way to get the first item from an associative array in JavaScript? JavaScript中解析文件中大量数据的最有效方法是什么 - What is the most efficient way in JavaScript to parse huge amounts of data from a file 将 object 的所有键转换为小写的最佳方法(最有效)是什么? - What's the best way (most efficient) to turn all the keys of an object to lower case? 从嵌套的 JavaScript 对象中检索属性的最有效方法是什么? - What is the most efficient way to retrieve properties from a nested JavaScript object? 在 JavaScript 中从对象复制某些属性的最有效方法是什么? - What is the most efficient way to copy some properties from an object in JavaScript? 在我的情况下,获得第一个更新值的有效方法是什么? - What's the efficient way to get the first updated value in my case? 在Javascript中声明函数的最有效方法是什么? - What is the most efficient way to declare functions in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM