I have this model:
[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; }
}
this is the controller 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);
}
This is the View:
@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>
}
}
This is the template from ViewsSharedEditorTemplatesDateTime.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"})
When I select a date and submit the form, the ModelState is false, and releaseDate comes with an error:
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…