ASP.Net MVC完整日历

时间:2018-09-28 15:20:34

标签: javascript asp.net-mvc

我已经执行了日历,但是遇到一些困难和问题。 当我选择一个事件并单击“编辑”按钮时...而不是向我显示要编辑的数据...该页面仅提供了简单的刷新,没有任何反应,我不知道为什么。另一个问题是,当我在日历中看到带有日期的日历事件时,我的目的是要显示名称或ID ...我不知道该在哪里修改它。

Problem

HTML

<div id="calender" style="width: 100%; height: 200%; display: inline-block;"></div>
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><span id="eventTitle"></span></h4>
                </div>
                <div class="modal-body">
                    <button id="btnDelete" class="btn btn-default btn-sm pull-right">
                        <span class="glyphicon glyphicon-remove"></span> Remover
                    </button>
                    <button id="btnEdit" class="btn btn-default btn-sm pull-right" style="margin-right:5px;">
                        <span class="glyphicon glyphicon-pencil"></span> Editar
                    </button>
                    <p id="pDetails"></p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                </div>
            </div>
        </div>
    </div>

    <div id="myModalSave" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Salvar Evento</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" id="hdID_Reserva" />
                        <div class="col-sm-6 form-group">
                            <label>Cliente</label>
                            <input class="form-control" type="text" placeholder="Introduza o Nome" id="txtID_Cliente">
                            @*<input type="hidden" id="txtID_Cliente" name="ID_Cliente" />*@
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Nº Telemóvel</label>
                            <input id="txtTelemovel" class="form-control" />
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Nº Cartão Cidadão</label>
                            <input id="txtCartaoCidadao" class="form-control" />
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Data de Entrada</label>
                            <div class="input-group date" id="dtp1">
                                <input type="text" id="txtDataEntrada" class="form-control" />
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                            </div>
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Data de Saida</label>
                            <div class="input-group date" id="dtp1">
                                <input type="text" id="txtDataSaida" class="form-control" />
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </span>
                            </div>
                        </div>
                        <div class="col-sm-6 form-group">
                            <label>Número Quarto</label>

                            @Html.DropDownList("ID_Quarto", null, "Selecione o Quarto", htmlAttributes: new { @class = "form-control" })
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Número Pessoas</label>
                            <input id="txtNumeroPessoas" class="form-control" />
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Número Noites</label>
                            <input id="txtNumeroNoites" class="form-control" />
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Preço</label>
                            <input id="txtPreço" class="form-control" />
                        </div>
                        <div class=" col-sm-6 form-group">
                            <label>Observações</label>
                            <input id="txtObservaçoes" class="form-control" />
                        </div>
                        <button type="button" id="btnSave" class="btn btn-success">Salvar</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

JavaScript

 <script>
            $(document).ready(function () {
                var events = [];
                var selectedEvent = null;
                FetchEventAndRenderCalendar();
                function FetchEventAndRenderCalendar() {
                    events = [];
                    $.ajax({
                        type: "GET",
                        url: "/AdelaideHotel/CalendárioReservas/GetEvents",
                        success: function (data) {
                            $.each(data, function (i, v) {
                                events.push({
                                    id: v.ID_Reserva,
                                    clienteID: v.ID_Cliente,
                                    quartoID: v.ID_Quarto,
                                    start: moment(v.DataEntrada),
                                    end: v.DataSaida != null ? moment(v.DataSaida) : null,
                                    noites: v.NumeroNoites,
                                    pessoas: v.NumeroPessoas,
                                    preço: v.Preço,
                                    obs: v.Observaçoes
                                });
                            });

                            GenerateCalender(events);
                        },
                        error: function (error) {
                            alert('failed');
                        }
                    })
                }

                function GenerateCalender(events) {
                    $('#calender').fullCalendar('destroy');
                    $('#calender').fullCalendar({
                        contentHeight: 900,
                        height: 650,
                        defaultDate: new Date(),
                        timeFormat: 'h(:mm)a',
                        header: {
                            left: 'prev,next today',
                            center: 'title',
                            right: 'month,basicWeek,basicDay,agenda,agendaWeek,agendaDay'
                        },
                        dayNames: ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sabado'],
                        dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab'],
                        monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outrubro', 'Novembro', 'Dezembro'],
                        monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Maio', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
                        allDayText: 'Dia inteiro',
                        buttonText: {
                            today: 'Hoje',
                            month: 'Mês',
                            week: 'Semana',
                            day: 'Dia'
                        },
                        droppable: true,
                        eventLimit: false,
                        eventColor: '#3c8dbc',
                        events: events,
                        eventClick: function (calEvent, jsEvent, view) {
                            selectedEvent = calEvent;
                            $('#myModal #eventTitle').text(calEvent.title);
                            var $quartoID = $('<div/>');
                            $quartoID.append($('<p/>').html('<b>DataEntrada:</b>' + calEvent.start.format("DD-MMM-YYYY HH:mm a")));
                            if (calEvent.end != null) {
                                $quartoID.append($('<p/>').html('<b>DataSaida:</b>' + calEvent.end.format("DD-MMM-YYYY HH:mm a")));
                            }
                            $quartoID.append($('<p/>').html('<b>ID_Quarto:</b>' + calEvent.quartoID));
                            $('#myModal #pDetails').empty().html($quartoID);

                            $('#myModal').modal();
                        },
                        selectable: true,
                        select: function (start, end) {
                            selectedEvent = {
                                id: 0,
                                clienteID: '',
                                quartoID: '',
                                start: start,
                                end: end,
                                pessoas: '',
                                noites: '',
                                preço: '',
                                obs: ''

                            };
                            openAddEditForm();
                            $('#calendar').fullCalendar('unselect');
                        },
                        editable: true,
                        eventDrop: function (event) {
                            var data = {
                                ID_Reserva: event.id,
                                ID_Cliente: event.clienteID,
                                DataEntrada: event.start.format('DD/MM/YYYY HH:mm A'),
                                DataSaida: event.end != null ? event.end.format('DD/MM/YYYY HH:mm A') : null,
                                ID_Quarto: event.quartoID,
                                NumeroNoites: event.noites,
                                NumeroPessoas: event.pessoas,
                                Preço: event.preço,
                                Observaçoes: event.obs,
                                Title: event.title
                            };
                            SaveEvent(data);
                        }
                    })
                }

                $('#btnEdit').click(function () {
                    //Open modal dialog for edit event
                    openAddEditForm();
                })
                $('#btnDelete').click(function () {
                    if (selectedEvent != null && confirm('Are you sure?')) {
                        $.ajax({
                            type: "POST",
                            url: '/AdelaideHotel/CalendárioReservas/DeleteEvent',
                            data: { 'eventID': selectedEvent.id },
                            success: function (data) {
                                if (data.status) {
                                    //Refresh the calender
                                    FetchEventAndRenderCalendar();
                                    $('#myModal').modal('hide');
                                }
                            },
                            error: function () {
                                alert('Failed');
                            }
                        })
                    }
                })

                $('#dtp1,#dtp2').datetimepicker({
                    format: 'DD/MM/YYYY HH:mm A'
                });

                function openAddEditForm() {
                    if (selectedEvent != null) {
                        $('#hdID_Reserva').val(selectedEvent.id);
                        $('#txtID_Cliente').val(selectedEvent.clienteID);
                        $('#txtDataEntrada').val(selectedEvent.start.format('DD/MM/YYYY HH:mm A'));
                        $('#txtDataSaida').val(selectedEvent.end != null ? selectedEvent.end.format('DD/MM/YYYY HH:mm A') : '');
                        $('#ID_Quarto').val(selectedEvent.quartoID);
                        $('#txtNumeroNoites').val(selectedEvent.noites);
                        $('#txtNumeroPessoas').val(selectedEvent.pessoas);
                        $('#txtPreço').val(selectedEvent.preço);
                        $('#txtObservaçoes').val(selectedEvent.obs);
                    }
                    $('#myModal').modal('hide');
                    $('#myModalSave').modal();
                }

                $('#btnSave').click(function () {
                    //Validation/
                    if ($('#txtID_Cliente').val().trim() == "") {
                        alert('Introduza um Cliente');
                        return;
                    }
                    if ($('#txtDataEntrada').val().trim() == "") {
                        alert('Introduza uma Data de Início');
                        return;
                    }

                    else {
                        var startDate = moment($('#txtDataEntrada').val(), "DD/MM/YYYY HH:mm A").toDate();
                        var endDate = moment($('#txtDataSaida').val(), "DD/MM/YYYY HH:mm A").toDate();
                        if (startDate > endDate) {
                            alert('Data de Fim Inválida');
                            return;
                        }
                    }

                    var data = {
                        ID_Reserva: $('#hdID_Reserva').val(),
                        ID_Cliente: $('#txtID_Cliente').val().trim(),
                        DataEntrada: $('#txtDataEntrada').val().trim(),
                        DataSaida: $('#txtDataSaida').val().trim(),
                        ID_Quarto: $('#ID_Quarto').val(),
                        NumeroPessoas: $('#txtNumeroPessoas').val(),
                        NumeroNoites: $('#txtNumeroNoites').val(),
                        Preço: $('#txtPreço').val(),
                        Observaçoes: $('#txtObservaçoes').val()
                    }
                    SaveEvent(data);
                    // call function for submit data to the server
                })

                function SaveEvent(data) {
                    $.ajax({
                        type: "POST",
                        url: '/AdelaideHotel/CalendárioReservas/SaveEvent',
                        data: data,
                        success: function (data) {
                            if (data.status) {
                                //Refresh the calender
                                FetchEventAndRenderCalendar();
                                $('#myModalSave').modal('hide');
                            }
                        },
                        error: function () {
                            alert('Failed');
                        }
                    })
                }
            })
        </script>

0 个答案:

没有答案