Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

c# - ModelState.IsValid = false using jquery datepicker

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:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

Using the TextBoxFor will wire up the model binding correctly.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...