私は多くの疑問のある質問を見つけましたが、うまくいきました。これを動作させるためのカスタムコードがたくさんあるのですが、それはなぜですか?これは最初からうまくいかないのでしょうか?MVC DateTimeの検証に失敗しました
私は奇妙だと思うものは、IE9では動作しますが、FirefoxとChromeでは動作しません。 FirefoxやChromeで試してみるたびに、「フィールド誕生日は日付でなければなりません」というメッセージが表示されます。
新しいMVC 4 RTMプロジェクトで以下のコードを試してみると、うまく動作しません。すべてのブラウザでDateTime.Nowのデフォルトがdd-MM-yyyy(オランダ)と表示されていますが、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")
}
が、私は上記のMVCビューの書式を設定する方法がわかりません。 –