繁体   English   中英

ASP.NET MVC如何使用模型属性的日期填充日期选择器

[英]ASP.NET MVC How to populate an editorfor date picker with the date of a model's property

我有一个网站,该网站是我在ASP.NET中使用MVC结构创建的,总体而言,它就像通过Web表单从数据库创建和显示数据的效率一样。 但是,我遇到的问题是Razor和EditorFor帮助程序。 我知道他们最近添加了发送HTML属性的功能,该功能已与Bootstrap的日期选择器一起使用,以实现更友好的输入系统。 但是,我找不到一种将现有记录的model.Sale.Date填充到字段中的方法(就像我对DropDownListFor帮助器所做的那样)。

 @Html.LabelFor(model => model.Sale.Date, new { @class = "control-label" }) 
 @Html.EditorFor(model => model.Sale.Date, new { @class = "form-control datepicker", placeholder = "Pick a date" }) 
 @Html.ValidationMessageFor(model => model.Sale.Date, "", new { @class = "text-danger" }) 

 @Html.LabelFor(model => model.Employee.FullName, htmlAttributes: new { @class = "control-label" }) 
 @Html.DropDownListFor(model => model.Employee.Id, new SelectList(Model.Employees.Employees, "Id", "FullName", Model.Sale.EmployeeId)) 
 @Html.ValidationMessageFor(model => model.Employee.Id, "", new { @class = "text-danger" }) 

 @Html.LabelFor(model => model.Service.Description, htmlAttributes: new { @class = "control-label" }) 
 @Html.DropDownListFor(model => model.Service.Id, new SelectList(Model.Services.Services, "Id", "Description", Model.Sale.ServiceId), null, null) 
 @Html.ValidationMessageFor(model => model.Service.Id, "", new { @class = "text-danger" }) 

有谁知道如何最简单地做到这一点的线索?

您可以使用JQueryui库并使用The DatePicker 更多信息

它具有许多功能,您可以在剃刀视图中轻松实现这些功能。 您的代码将如下所示:

 @Html.JQueryUI().DatepickerFor(model => model.SaleDate, new { @class = "form-control" })
 @Html.ValidationMessageFor(model => model.SaleDate)

我这样解决这个问题: <input id="FechaCita" name="FechaCita" type="date" class="form-control mr-2" min="@Model.SetUpMinMax.ToString("yyyy-MM-dd")" value="@Model.FechaCita.ToString("yyyy-MM-dd")" />

当我在Web浏览器的控制台中进行测试时,我发现浏览器中具有日期的格式与我使用的格式不同,因此我更改了格式以进行查看,并且一切正常

但是即使当我必须使用任务而不是剃刀的@html帮助程序时,因为它再次传递了发送的格式。 我可以使用注释格式,但是我用英语保存日期,所以我就这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM