Using datepicker in MVC 4.0 to store date value in db, throwing "object does not support this property or method"

Work with MVC 4.0 / Razor as a viewer.

Scenario: binding a calendar to a text field, clicking in txtBox should let the calendar stream down using jqueryUI datepicker and sending it to sql db.

What I've done:

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; } } 

Shaver Type:

 @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 the view is loaded, "the object does not support this property or method."

+1
source share
1 answer

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

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

0
source

Source: https://habr.com/ru/post/956045/


All Articles