问题
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