MVC 5-在所有浏览器中接受日期字段
MVC 5 - Accepting Date Field Across All Browsers
你好,我目前在MVC 5中接受所有4种浏览器、Internet Explorer、Chrome、Edge和Safari的日期时感到非常痛苦。
我正在使用Jquery ui日期选择器。
根据我目前的代码和下面列出的设置,我可以接受Internet Explorer和Chrome、Edge和Safari中的日期返回"购买日期:必须是日期"
我的技能很基本,但在过去的一年里,我学到了很多,但老实说,我更沮丧的是,像数据字段这样简单的东西会让我如此痛苦,搜索只是让我绕圈子转,我有工作代码,但不是所有浏览器都有。
如果有人能为我指出一个类似的问题的正确方向(我看了一整天都找不到),或者找到一个涵盖所有基础的解决方案,或者只是帮助我照亮昏暗的天空,我将不胜感激。
My Field在其模型中声明如下。
[Required]
[Display(Name = "Date of Purchase:")]
[DisplayFormat(DataFormatString = "{0:yyyy/MMM/dd}",
ApplyFormatInEditMode = true)]
public System.DateTime date_of_purchase { get; set; }
我的web.config声明全球化如下:
<globalization culture="auto" uiCulture="auto" />
在我的HTML Razor页面上,我调用Jquery Datepicker如下:
<script>
$(document).ready(function () {
$("#@Html.IdFor(m => m.date_of_purchase)").datepicker({dateFormat: 'yy/MM/dd', maxDate: "0" });
});
</script>
日期输入的表单字段如下:
<div class="form-group">
@Html.LabelFor(model => model.date_of_purchase, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.date_of_purchase, new { htmlAttributes = new { @class = "form-control", @readonly = "true", @placeholder = "Required" } })
@Html.ValidationMessageFor(model => model.date_of_purchase, "", new { @class = "text-danger" })
</div>
</div>
我的控制器在下面,但我认为这与问题沼泽标准无关。
public async Task<ActionResult> VehicleRegistration([Bind(Include = "User_Email,Serial_No,IMEI_No,dealer_id,dealer_name,date_of_purchase")] VehicleRegistration vehicleReg)
{
if (ModelState.IsValid)
{
IQueryable<Dealer> dealerSort;
dealerSort = db.Dealers.Where(o => o.Name == vehicleReg.dealer_name);
vehicleReg.dealer_id = dealerSort.Single().DealerID;
IQueryable<AspNetUser> UserSort;
UserSort = db.AspNetUsers.Where(o => o.Email == vehicleReg.User_Email);
string userSortResult = UserSort.Single().Id;
string userTitle = UserSort.Single().Title;
string userSurname = UserSort.Single().Surname;
string userTel = UserSort.Single().Home_Phone;
string userMob = UserSort.Single().Mobile_Phone;
string callbackUrl = await SendEmailVehicleRegistrationRequest(userSortResult, "Vehicle Registration Request", vehicleReg.User_Email, vehicleReg.Serial_No, vehicleReg.IMEI_No, vehicleReg.dealer_id, vehicleReg.dealer_name, vehicleReg.date_of_purchase, userTitle, userSurname, userTel, userMob);
ViewBag.Message = "An email has been sent to " + vehicleReg.dealer_name + ", requesting them to complete the sale of your vehicle." +
" A copy has also been sent to Swift Command Support, if you do not receive confirmation within 3 days please contact Swift Command Support.";
return View("Info");
}
return View(vehicleReg);
}
这个解决方案要感谢@StephenMuecke发现了我的问题。
如果我的模型我修改了它,并将月份从MMM更改为下面的MM新代码。
[Required]
[Display(Name = "Date of Purchase:")]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public System.DateTime date_of_purchase { get; set; }
在为Jquery日期选择器设置的页面上,我将日期格式更改为"dd/mm/yy",这是我一直想要的,但无法在两个浏览器之间工作,更不用说所有5个了。它以前是"yy/MM/dd"。
<script>
$(document).ready(function () {
$("#@Html.IdFor(m => m.date_of_purchase)").datepicker({ dateFormat: 'dd/mm/yy', maxDate: "0" });
});
</script>
多亏了我认识的每个人的输入,它以IE、Edge、Chrome、FireFox和Safari所需的日期格式工作。
相关文章:
- 如何使toLocalDateString中的日期字段可选
- 如何使用属性禁用日期字段
- JSP 中的“日期”字段存在问题
- 无法格式化keystone.js中的嵌套日期字段
- mongodb mongodump json日期字段解析错误
- 在日期字段上使用FTSearch
- crm 2011 javascript将日期字段设置为null
- 如何使用Javascript从HTML中的日期字段捕获日期
- ExtJS 5.1-如何将日期字段屏蔽为00/00/0000
- 用于比较日期字段的Javascript
- MVC 5-在所有浏览器中接受日期字段
- 在django中提交表单时更改日期字段值
- SQLite:从Phonegap/Cordova中的(文本)日期字段中选择日期和时间
- j查询验证单独的年、月、日的日期字段
- 从替代字段填充 jquery UI 日期选择器日期字段
- 在输入日期字段的多次迭代中加载日期选取器
- 在 podio 中将计算日期字段设置为 null(因此使用 javascript)
- SlickGrid:单元格中具有两个日期字段的复合编辑器
- MongoDB中的日期字段类型(并对其进行排序)
- MVC5 - 检查用于搜索日期字段的 2 个日期(文本框)之间的重叠