[英]DateTime field is null when passing to controller in ASP.NET MVC5
I have in my application a search form using AjaxBeginForm
.我的应用程序中有一个使用AjaxBeginForm
的搜索表单。 In the same I have a date field ( DateTime
), and in which I am using the datePicker bootstrap.同样,我有一个日期字段( DateTime
),并且我在其中使用了 datePicker 引导程序。
My form looks like this:我的表格是这样的:
@using (Ajax.BeginForm("Index", "Pedido", new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "divPedidos" }, new { @class = "form-inline" }))
{
@Html.TextBox("pesquisarRascunhoId", 0, new { style = "display: none" })
<div class="row">
<div class="col-sm-4">
<p>Data emissão</p>
<div class="input-group date col-sm-5">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
@Html.EditorFor(model => model.PedidoFilter.EmissaoInicial, new { htmlAttributes = new { @class = "form-control input-sm datepicker" } })
</div>
a
<div class="input-group date col-sm-5">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
@Html.EditorFor(model => model.PedidoFilter.EmissaoFinal, new { htmlAttributes = new { @class = "form-control input-sm datepicker" } })
</div>
</div>
<div class="btn-search">
<p class="demo-button">
<button type="submit" class="btn btn-success"><i class="fa fa-search"></i><span>Pesquisar</span></button>
</p>
</div>
}
My javaScript for formatting my date in dd / mm / yyyy looks like this:我的用于在 dd / mm / yyyy 中格式化日期的 javaScript 如下所示:
$(".datepicker").datepicker({
format: "dd/mm/yyyy",
language: "pt-BR",
autoclose: true
});
My entity looks like this:我的实体看起来像这样:
public class PedidoFilter
{
public DateTime? EmissaoInicial { get; set; }
public DateTime? EmissaoFinal { get; set; }
}
And finally my controller looks like this:最后我的控制器看起来像这样:
public ActionResult Index(PedidoResult pedidoResult,
{
var pedido = _service.FindAll(pedidoResult.PedidoFilter);
return View(pedido);
}
I define my language culture in my Web.config like this:我在我的 Web.config 中定义我的语言文化,如下所示:
<globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" />
All fields of my form arrive in my controller, but my date fields are always null, in the format dd/mm/yyyy
, which I define in my datepicker js.我的表单的所有字段都到达我的控制器,但我的日期字段始终为空,格式为dd/mm/yyyy
,这是我在我的日期选择器 js 中定义的。
If I enter a date in the mm/dd/yyyy
format it is passed to my controller.如果我以mm/dd/yyyy
格式输入日期,它会传递给我的控制器。
My question is:我的问题是:
How can I pass my date field in the dd/mm/yyyy
format and get them on my controller without them being null?如何以dd/mm/yyyy
格式传递我的日期字段并将它们放在我的控制器上而不为空?
I think the date format can be changed with Culture or via the code example below: C# takes the first month of the year after the day.我认为可以使用 Culture 或通过下面的代码示例更改日期格式:C# 将一年中的第一个月放在一天之后。 You solve this problem.你解决这个问题。 Use the flatpickr or other datepicker.使用 flatpickr 或其他日期选择器。
<input asp-for="Birthdate" type="datetime" data-name="date-picker" class="form-control" autocomplete="off">
let birthDay = flatpickr(document.getElementById('Birthdate'), { enableTime: true, dateFormat: 'dm-Y', });
$('#myform').submit((e) => { $('#Birthdate').val(birthDay.latestSelectedDateObj.toLocaleString()); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.