【问题标题】:Is there a delicate way to pass calendar events variables in Javascript是否有一种微妙的方法可以在 Javascript 中传递日历事件变量
【发布时间】:2021-01-29 06:51:20
【问题描述】:

从下面的示例中可以看出,我有一个包含数据的@model

为了使用数据的特定部分,我使用 Json.Serialize 将其分配给 Json 对象。我这样做的原因是 events:[ ] 部分接受某种特定格式的数据,看起来像一种 json 格式。

正如您在events:[ ] 部分中看到的,我尝试以这样一种方式提取数据,以便程序的events:[ ] 部分接受它。 (一切正常)

问题在于,json 中的记录数会随着它从数据库表中提取数据而变化。

我想要的结果是:在events:[ ] 部分中引入必要数据的更好公式。

可能有一种正确使用 json 的方法,但我还没有找到。如果您对此有任何想法,我会很高兴收到您的来信。

@model IEnumerable<WebAppV2.Models.CalendarEvent>


<script>

    document.addEventListener('DOMContentLoaded', function () {
        var calendarEl = document.getElementById('calendar');


        var json = @Html.Raw(Json.Serialize(@Model));


        var calendar = new FullCalendar.Calendar(calendarEl, {
            initialDate: '2021-01-01',
            editable: false,
            selectable: false,
            businessHours: true,
            dayMaxEvents: true,
            events: [


                {
                    title: json[0].title,
                    start: json[0].start,
                    end: json[0].end
                },

                {
                    title: json[1].title,
                    start: json[1].start,
                    end: json[1].end
                },

                {
                    title: json[2].title,
                    start: json[2].start,
                    end: json[2].end
                },


            ]
        });

        calendar.render();
    });

</script>

【问题讨论】:

    标签: javascript json .net


    【解决方案1】:

    使用此解决方案。

    var events = @Html.Raw(Json.Serialize(@Model.Select(e => new
    {
    e.title,
    e.start,
    e.end
    })));
    

    以后

    var calendar = new FullCalendar.Calendar(calendarEl, {
        initialDate: '2021-01-01',
        editable: false,
        selectable: false,
        businessHours: true,
        dayMaxEvents: true,
        events: events
    });
    
    calendar.render();
    

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 2016-02-28
      • 2018-07-01
      • 1970-01-01
      相关资源
      最近更新 更多