asp.net-mvc – MVC DateTime验证失败
发布时间:2020-05-22 12:20:07 所属栏目:asp.Net 来源:互联网
导读:我发现了许多simulair问题,但不是一个有效的干净解决方案.我看到很多自定义代码可以让它工作,但为什么会这样?这应该从一开始就不起作用吗? 我认为奇怪的是,在IE9中它可以工作,但在Firefox和Chrome中它失败了. 每当我在Firefox或Chrome中尝试时,我都会收到消
|
我发现了许多simulair问题,但不是一个有效的干净解决方案.我看到很多自定义代码可以让它工作,但为什么会这样?这应该从一开始就不起作用吗? 我认为奇怪的是,在IE9中它可以工作,但在Firefox和Chrome中它失败了. 当我在新的MVC 4 RTM项目中尝试下面的代码时,我无法让它工作.我在所有浏览器中都看到DateTime.Now默认为dd-MM-yyyy(Holland)但我无法在Firefox和Chrome中提交它. 全局标记未在web.config中设置,因此必须使用默认值.我来自荷兰,所以它应该得到我猜的客户文化. public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:d}",ApplyFormatInEditMode = true)]
//[DataType(DataType.Date)]
public DateTime Birthday { get; set; }
}
[AllowAnonymous]
public ActionResult Register()
{
RegisterModel vm = new RegisterModel()
{
Birthday = DateTime.Now
};
return View(vm);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
//WebSecurity.CreateUserAndAccount(model.UserName,model.Password);
//WebSecurity.Login(model.UserName,model.Password);
return RedirectToAction("Index","Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("",ErrorCodeToString(e.StatusCode));
}
}
// If we got this far,something failed,redisplay form
return View(model);
}
标记 <!-- language: lang-none -->
@model DateTimeWithDatePicker.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Birthday)
@Html.EditorFor(m => m.Birthday)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
解决方法我能够通过修改日期的jQuery验证器函数来解决这个问题:<script type="text/javascript">
$(function () {
var dateFormat="dd/mm/yy"; // en-gb date format,substitute your own
$("#Birthday").datepicker({
"dateFormat": dateFormat
});
jQuery.validator.addMethod(
'date',function (value,element,params) {
if (this.optional(element)) {
return true;
};
var result = false;
try {
$.datepicker.parseDate(dateFormat,value);
result = true;
} catch (err) {
result = false;
}
return result;
},''
);
}); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET MVC中的单元测试比Web窗体更好?
- 在ASP.Net MVC中禁用会话状态每请求
- ASP.NET MVC Model绑定的简单应用
- asp.net – 错误4005表单身份验证失败 – 提供的故障单已过
- asp.net-mvc-3 – 删除Razor MVC 3中的HTML格式
- asp.net – CSS中的内联样式与样式
- asp.net-mvc – Linq选择语句 – 不在的地方
- asp.net-mvc – 具有Fluent nHibernate和Ninject的多租户.每
- asp.net-core – AspNetCore Abstractions无法加载
- asp.net – 如何防止网页上的CSS缓存?
推荐文章
站长推荐
热点阅读
