Using datepicker in MVC 4.0 to persist a date value in db, throwing “object doesn't support this property or method”

依然范特西╮ 提交于 2020-01-16 01:00:39

问题


Working around with MVC 4.0/Razor as the view engine.

Scenario: Binding a calendar to a text box, clicking in the txtBox should let a calendar flow down,using jqueryUI's datepicker and submitting it to a sql db.

What I did:

Model class:

    public class UserModels
    {

        [Required(ErrorMessage = "Mandatory Field")]
        public virtual string Name { get; set; }
        public virtual int Id { get; set; }
        [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public virtual DateTime Dob { get; set; }
        [Display(Name = "CountrY")]
        public virtual string Country { get; set; }
        public virtual string Gender { get; set; }


    }

Razor View:

@model EMSapp.Models.UserModels

@{
    ViewBag.Title = "AddUser";
}
<h2>AddUser</h2>
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.core.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.datepicker.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {

        $("#date").datepicker();

    }); 
    </script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>UserModels</legend>
         <div class="editor-label">
            @Html.LabelFor(model => model.Dob)
        </div>
        <div class="editor-field">

            @**Html.TextBoxFor(model => model.Dob, new { id = "date" })**   

        </div>

Error: When is the view is loading, "object doesn't support this property or method" is thrown.


回答1:


Html.TextBoxFor(model => model.Dob, new { id = "date" }) should be setting the name and id attributes of your textbox to "Dob".

Try calling Html.TextBoxFor(model => model.Dob) and using $('#Dob').datepicker(); instead.



来源:https://stackoverflow.com/questions/16148765/using-datepicker-in-mvc-4-0-to-persist-a-date-value-in-db-throwing-object-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!