ModelState.IsValid = FALSE使用jQuery日期选择器 [英] ModelState.IsValid = false using jquery datepicker
问题描述
我有这样的模型:
[MetadataType(typeof(MovieMetadata))]
public partial class Movie
{
}
class MovieMetadata
{
[ScaffoldColumn(false)]
public int id { get; set; }
[Required(ErrorMessage = "Title is required")]
public string title { get; set; }
[Required]
public DateTime releaseDate { get; set; }
public string storyline { get; set; }
public Binary poster { get; set; }
[ScaffoldColumn(false)]
public DateTime? duration { get; set; }
[ScaffoldColumn(false)]
public Binary trailer { get; set; }
}
这是控制器code:
[HttpPost]
public ActionResult Create([Bind(Exclude = "poster, trailer")]Movie movie, HttpPostedFileBase poster, HttpPostedFileBase trailer)
{
if (ModelState.IsValid)
{
//saving the movie
OperationStatus opStatus = Repository.Save(movie);
if (!opStatus.Status)
{
return View("Error");
}
}
return View(movie);
}
这是视图:
@model MoviesModel.Movie
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/MoviesLayout.cshtml";
}
@section createMovie{
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createForm", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="gallMemberBox">
<div class="leftFormContent">
<a href="#">Movie Name</a>
<div class="imgTmpl">
<!--solo hay que especificar el src de la imagen-->
<img src="../../Content/img/imgTest.jpg" alt="" />
</div>
</div>
<div class="rightFormContent">
<div>
@Html.LabelFor(model => model.title)
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
<div>
@Html.LabelFor(model => model.releaseDate)
@Html.EditorFor(model => model.releaseDate)
@Html.ValidationMessageFor(model => model.releaseDate)
</div>
<input type="submit" value="Create" />
</div>
<div class="clearBoth"></div>
</div>
}
}
这是从视图模板\\共享\\ EditorTemplates \\ DateTime.cshtml
This is the template from Views\Shared\EditorTemplates\DateTime.cshtml
@Styles.Render("~/Content/themes/base/jquery-ui.css")
@Scripts.Render("~/Scripts/jquery-ui-1.8.24.js")
<script>
$(function () {
$("#datepicker").datepicker();
});
</script>
@Html.TextBox("datepicker", null, new{id="datepicker"})
当我选择日期并提交表单,该ModelState中是假的,RELEASEDATE配备了一个错误:
When I select a date and submit the form, the ModelState is false, and releaseDate comes with an error:
推荐答案
如果您要使用编辑模板,改变它的东西是这样的:
If you want to use the editor template, change it to something like this:
@model DateTime?
@Styles.Render("~/Content/themes/base/jquery-ui.css")
@Scripts.Render("~/Scripts/jquery-ui-1.8.24.js")
@Html.TextBoxFor(m => m, new{@class="datepicker"})
<script>
$(function () {
$(".datepicker").datepicker();
});
</script>
使用 TextBoxFor
将线了该模型正确绑定。
Using the TextBoxFor
will wire up the model binding correctly.
另外,我建议您移动 Styles.Render()
和 Scripts.Render()
您 _layout
。因为我改变了日期选择器()
要连接一个类,你也可以移动,如果你想要的。
Also, I would recommend moving your Styles.Render()
and Scripts.Render()
to your _Layout
if possible. Since I changed the datepicker()
to wire up a class, you could also move that if you wanted.
这篇关于ModelState.IsValid = FALSE使用jQuery日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!