ASP.Net MVC从FullCalendar的javascript函数调用控制器动作而不加载视图

时间:2022-12-01 22:48:32

I am developing an ASP.Net MVC application. In one of my views, I am using FullCalendar control. I am having 2 issues with my fullcalendar control - My First issue is - On event click function, I am calling an action of some other controller from a Javasript function. My script is -

我正在开发一个ASP.Net MVC应用程序。在我的一个观点中,我正在使用FullCalendar控件。我的fullcalendar控件有2个问题 - 我的第一个问题是 - 在事件点击功能上,我从Javasript函数调用一些其他控制器的动作。我的剧本是 -

    <script>
        $(document).ready(function () {

            var sourceFullView = { url: '/Calendar/GetDiaryEvents/' };
            var sourceSummaryView = { url: '/Calendar/GetDiaryEvents/' };
            var CalLoading = true;

            $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month'
                },
                defaultView: 'month',
                editable: true,
                allDaySlot: false,
                selectable: true,
                slotMinutes: 15,
                events: '/Calendar/GetDiaryEvents/',
                eventClick: function (calEvent, jsEvent, view) {
                    if (!confirm('You selected Election ID: ' + calEvent.title
                        + "\nElection Date: " + calEvent.start.toDateString()
                        + "\n\nDo you want to Vote for this Election?")) {
                        return;
                    }
                    var electionID = calEvent.id;
                    $.ajax({
                        url: '@Url.Action("Tally", "Voting")',
                        type: 'GET',
                        dataType: 'json',
                        cache: false,
                        data: { id: calEvent.id }
                    });
                },

                eventResize: function (event, dayDelta, minuteDelta, revertFunc) {

                    if (confirm("Confirm change appointment length?")) {
                        UpdateEvent(event.id, event.start, event.end);
                    }
                    else {
                        revertFunc();
                    }
                },

                viewRender: function (view, element) {

                    if (!CalLoading) {
                        if (view.name == 'month') {
                            $('#calendar').fullCalendar('removeEventSource', sourceFullView);
                            $('#calendar').fullCalendar('removeEvents');
                            $('#calendar').fullCalendar('addEventSource', sourceSummaryView);
                        }
                    }
                }

            });

            CalLoading = false;


        });

    </script>

This is calling Tally method of VotingController -

这是调用VotingController的Tally方法 -

            [HttpGet]
            public ActionResult Tally(int id)
            {
                return View(id);
            }

When I debug through View of Tally then also it is looking fine but the new view called from action Tally is not loaded and browser still shows my calendar control.

当我通过View of Tally进行调试时,它也看起来很好但是从操作Tally调用的新视图未加载,浏览器仍显示我的日历控件。

My second issue is I want to display only monthly calendar. When I click next or prev button on calendar then my GetDiaryEvents function is called thrice.

我的第二个问题是我想只显示月历。当我点击日历上的next或prev按钮时,我的GetDiaryEvents函数被调用三次。

1 个解决方案

#1


0  

Try to use a JsonResult in the Tally method. You could also render the view to string and include it in the json return.

尝试在Tally方法中使用JsonResult。您还可以将视图呈现为字符串并将其包含在json返回中。

[HttpGet]
        public JsonResult Tally(int id)
        {   //fill your model
            return Json( new { viewdata=model } );
        }

And in the ajax call return you just need to read json return object

并且在ajax调用返回时你只需要读取json返回对象

 success: function (return) {
            $("#element").append(return.viewdata);

        }

#1


0  

Try to use a JsonResult in the Tally method. You could also render the view to string and include it in the json return.

尝试在Tally方法中使用JsonResult。您还可以将视图呈现为字符串并将其包含在json返回中。

[HttpGet]
        public JsonResult Tally(int id)
        {   //fill your model
            return Json( new { viewdata=model } );
        }

And in the ajax call return you just need to read json return object

并且在ajax调用返回时你只需要读取json返回对象

 success: function (return) {
            $("#element").append(return.viewdata);

        }