javascript - Kendo Grid 中的日期字段在 Controller 上为空

标签 javascript c# asp.net-mvc kendo-ui

以下代码在开发中运行良好,但当我们部署到生产中时,字段 fecha(最初是日期时间)变为空。

我们甚至尝试更改为字符串而不是日期时间,但它仍然无法在我们的客户服务器上运行

我们的局部 View 是这样的: fecha.chstml

@using xx.Relacionamiento.Modelo.Bussiness.Entities
@model EventoEducacionFecha
@using Kendo.Mvc.UI

<script type="text/javascript">
    $(function () {
        kendo.culture("es-CO");
    })

    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }

    function getFecha() {
        debugger
        var fecha = $("#FechaEvent").val();
        return {
            FechaEvent: fecha
        };
    }
</script>
@(Html.Kendo().Grid<EventoEducacionFecha>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.EventoEducacionFechaId).Hidden();
        columns.Bound(p => p.FechaEvent).Title("Fecha Evento").ClientTemplate("#= kendo.toString(kendo.parseDate(FechaEvent, 'yyyy-MM-dd'), 'MM/dd/yyyy') #");
        columns.Bound(p => p.HoraInicio);
        columns.Bound(p => p.HoraFin);
        columns.Command(command =>
        {
            command.Edit().Text("Editar"); 
            //command.Destroy(); 
            command.Custom("Borrar").Click("openWindowConfirmDelete").HtmlAttributes(new { data_NomCol = "FechaEvent" });
        }).Width(250).Title("Acciones");
    })
    .ToolBar(toolbar => toolbar.Create().Text("Agregar Fecha"))
    .Editable(editable => editable.Mode(GridEditMode.InLine).DisplayDeleteConfirmation(false))
    .Pageable()
    .Sortable()
    .Scrollable()
    //.HtmlAttributes(new { style = "height:550px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => 
        { 
            model.Id(p => p.EventoEducacionFechaId); 
            model.Field(a => a.EventoEducacionFechaId).Editable(false);
            model.Field(a => a.FechaEvent).Editable(true);
            model.Field(a => a.HoraInicio).Editable(true);
            model.Field(a => a.HoraFin).Editable(true); 
        })
        .Create(update => update.Action("Fechas_Create", "EventosEducacion").Data("getFecha"))
        .Read(read => read.Action("GetFechas", "EventosEducacion").Data("getDatoEventoId"))
        .Update(update => update.Action("Fecha_Update", "EventosEducacion"))
        .Destroy(update => update.Action("Fecha_Destroy", "EventosEducacion"))
    )
)

这是使用局部 View 的 View 的PART

<div class="row oculto" id="VerFecha">
                <div class="col-md-12">
                    <div class="form-group">
                        <div id="mostrarFecha_div"></div>
                        @*@Html.Partial("~/Views/EventosEducacion/Fechas.cshtml",null,null)*@
                    </div>
                </div>

这是 Controller Action

//[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Fechas_Create([DataSourceRequest] DataSourceRequest request, EventoEducacionFecha EducaFecha, string FechaEvent)
        {
            if (EducaFecha.FechaEvent != null && EducaFecha.HoraInicio != null && EducaFecha.HoraFin != null)
            {
                LstEventoEducacionFecha.Add(new EventoEducacionFecha { 
                EventoEducacionFechaId = Guid.NewGuid(),
                EventoId = Guid.Empty,
                HoraFin = EducaFecha.HoraFin,
                FechaEvent = DateTime.Parse(FechaEvent),
                HoraInicio = EducaFecha.HoraInicio,
                });
                EducaFecha.EventoEducacionFechaId = LstEventoEducacionFecha.OrderBy(o => o.EventoEducacionFechaId).Select(s => s.EventoEducacionFechaId).FirstOrDefault();
                return Json(new[] { EducaFecha }.ToDataSourceResult(request));
            }
            return Json(new[] { EducaFecha }.ToDataSourceResult(request));
        }

最佳答案

过去,我遇到过 Kendo 的库以及与 DateTime 相关的任何问题。您通常必须将发送给 Controller 的 DateTime 从 JavaScript 的 UTC 时间格式转换为 c# 可以理解的格式。否则,将其作为字符串发送并查看它是否仍然为 null 或为空。我有一种感觉,它会以字符串的形式发送一些东西,你可以在服务器端转换它。

过去,对于 Kendo DateTime Picker,我必须在 js 中执行以下客户端操作:

 var meetDatePicker = $("#MeetingDate").data("kendoDatePicker");
 var mDate = new Date(meetDatePicker.value());
 var meetDate = mDate.toUTCString();

然后将 meetDate 作为字符串传递到我的 Controller ,并在服务器端使用 c#:

DateTime meetDateConv = DateTime.Parse(meetingDate);

关于javascript - Kendo Grid 中的日期字段在 Controller 上为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37792517/

相关文章:

javascript - 使用 Google Maps API 将纬度和经度转换为街道地址

JavaScript 的日期

javascript - 使用原生 javascript 从 json 解析 html 代码

c# - 使用 System.Text.Json 反序列化不可变记录数组

c# - 用户登录失败。尝试访问 SQL Server Express 数据库

c# - 在 ASP.NET MVC 中将模型从 View 传递到 Controller

javascript - 通过 Javascript 访问 ViewBag 列表

javascript - 移动剪裁的 Canvas

c# - 来自 C# Windows 服务的 Facebook 状态更新

asp.net-mvc - 如何使 Html.DisplayFor 显示我从数据库读取的 HTML?