我很难解决这个问题(而且我确信我在某处犯了一些愚蠢的错误:))有人可以帮忙。
提交时,我无法检索在模型中使用 JQuery datepicker 选择的日期。它给我的价值是“1/1/0001 12:00:00 AM”。我错过了什么吗?
为了隔离我的问题,我使用 Visual Studio 2010 中的模板创建了一个简单的 MVC3 应用程序。详细信息如下:
模型:
namespace DatePickerApp.Models
{
public class DatePickerClass
{
public DateTime BirthDate { get; set; }
}
}
控制器:
namespace DatePickerApp.Controllers
{
public class BirthDateController : Controller
{
//
// GET: /BirthDate/Create
public ActionResult Create()
{
return View();
}
//
// POST: /BirthDate/Create
[HttpPost]
public ActionResult Create(DatePickerApp.Models.DatePickerClass DatePickerClass)
{
try
{
// TODO: Add insert logic here
return View();
}
catch
{
return View();
}
}
}
}
cshtml 视图:
@model DatePickerApp.Models.DatePickerClass
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#date").datepicker({ dateFormat: 'dd/mm/yy' });
});
</script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>DatePickerClass</legend>
<div class="editor-label">
@Html.LabelFor(model => model.BirthDate)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.BirthDate, new { id = "date" })
@Html.ValidationMessageFor(model => model.BirthDate)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>