Jquery 单击日历时将数据加载到PartialView(ASP.NET MVC)

Jquery 单击日历时将数据加载到PartialView(ASP.NET MVC),jquery,asp.net,asp.net-mvc,asp.net-mvc-4,fullcalendar,Jquery,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Fullcalendar,我正在使用fullcalendar插件 我可以单击日历中的日期并从中获取数据 以下是脚本代码: <script> //Calendar initialization $(document).ready(function () { $('#calendar').fullCalendar({ header: { left: 'prev,next', center: 'title',

我正在使用fullcalendar插件

我可以单击日历中的日期并从中获取数据

以下是脚本代码:

<script>             //Calendar initialization
$(document).ready(function () {
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        defaultView: 'agendaWeek',
        events:"/Calendar/GetEvents/",
        firstDay: 1,
        selectable: true,
        select: function (start, end, jsEvent, view) {
            $('#right2').load('@Url.Action("SheduleNewAppointment", "Calendar")');
            var allDay = !start.hasTime() && !end.hasTime();
            $("#startAppointment").val(moment(start).format("MM/DD/YYYY HH:mm a"));

        },
        editable: true
    });
});
我的问题是,我需要在“选择”中打开“局部视图”,并将数据添加到“开始视图”。这是输入

现在它打开部分视图,而不是向输入添加数据。 我怎么能做到

更新


如果我用另一个按钮打开“部分视图”,然后在“部分视图”可见时单击“日历”,我会同意Lazys的评论。您需要了解ajax操作(如.load)是异步运行的,在异步操作调用下面的行中编写的任何代码都将立即执行,而无需等待异步操作完成

因此,您的代码正在尝试访问StartAppoint元素,并在该元素实际存在于页面之前设置其值

如果要运行的代码依赖于ajax调用返回的数据或标记,那么必须等待ajax调用完成后才能运行它。这是保证代码具有正确执行所需值的唯一方法。这通常是通过一个回调函数来完成的,您将回调函数提供给进行ajax调用的代码,然后将该函数存储起来,并且仅在ajax调用成功完成时执行

对于jQuery的.load方法,可以提供回调函数作为load方法本身的参数,如下所示:

$('#right2').load(
  '@Url.Action("SheduleNewAppointment", "Calendar")', 
  function () { //this is the callback function. It's not executed at the time you pass it load, but only after the loading operation (done via ajax) has finished
    var allDay = !start.hasTime() && !end.hasTime(); //I've included this line, although it appears to be redundant, since you never use the "allDay" variable afterwards.
    $("#startAppointment").val(moment(start).format("MM/DD/YYYY HH:mm a"));
  }
}); 
有关更多信息,请参阅


如果您这样做,它保证您的部分页面将在尝试访问该元素的jQuery代码之前加载,包括StartPampoint元素,那么您应该没有问题。

id为StartPampoint的元素在您正在加载的部分视图上?是,这是局部的view@LazysI相信您在JQuery加载函数的异步执行方面有问题。尝试将与设置StartAppoint值相关联的代码移动到load函数的回调。它应该是这样的:$'right2'。加载'@Url.actionScheduleNewAppointment,Calendar',函数{$startappoint.valmomentstart.formatMM/DD/YYYY HH:mm a;};
$('#right2').load(
  '@Url.Action("SheduleNewAppointment", "Calendar")', 
  function () { //this is the callback function. It's not executed at the time you pass it load, but only after the loading operation (done via ajax) has finished
    var allDay = !start.hasTime() && !end.hasTime(); //I've included this line, although it appears to be redundant, since you never use the "allDay" variable afterwards.
    $("#startAppointment").val(moment(start).format("MM/DD/YYYY HH:mm a"));
  }
});