控制器操作方法上的 Jquery 日期选择器值
我创建了 Jquery datepicker 的扩展方法,下面是源代码。
public static string DatePicker(this HtmlHelper htmlHelper, string name, DateTime? value)
{
if (!string.IsNullOrEmpty(name))
{
return "<script type=\"text/javascript\">"
+ "$(function() {"
+ "$(\"#" + name +
"\").datepicker({changeMonth: true,changeYear: true, dateFormat: 'dd/mm/yy', duration: 0});"
+ "});"
+ "</script>"
+ "<input type=\"text\" size=\"10\" value=\""
+ FormattedDate(value)
+ "\" id=\""
+ name.Replace('.', '_')
+ "\" name=\""
+ name + "\"/>";
}
return string.Empty;
}
我在View中使用了上面的扩展方法。
<div class="editor-label">
@Html.LabelFor(model => model.Committee.AppointedDate)
</div>
<div class="editor-field">
@Html.Raw(Html.DatePicker("CommitteeAppointedDate", Model.Committee.AppointedDate))
@Html.ValidationMessageFor(model => Model.Committee.AppointedDate)
</div>
当我单击提交按钮时,保存在指定日期之上的视图模型在控制器操作方法中显示 DateTime.MinValue
。
请让我们知道上面的代码有什么问题。
I've created a extension method of Jquery datepicker, below are the source code.
public static string DatePicker(this HtmlHelper htmlHelper, string name, DateTime? value)
{
if (!string.IsNullOrEmpty(name))
{
return "<script type=\"text/javascript\">"
+ "$(function() {"
+ "$(\"#" + name +
"\").datepicker({changeMonth: true,changeYear: true, dateFormat: 'dd/mm/yy', duration: 0});"
+ "});"
+ "</script>"
+ "<input type=\"text\" size=\"10\" value=\""
+ FormattedDate(value)
+ "\" id=\""
+ name.Replace('.', '_')
+ "\" name=\""
+ name + "\"/>";
}
return string.Empty;
}
I have used the above extension method in the View.
<div class="editor-label">
@Html.LabelFor(model => model.Committee.AppointedDate)
</div>
<div class="editor-field">
@Html.Raw(Html.DatePicker("CommitteeAppointedDate", Model.Committee.AppointedDate))
@Html.ValidationMessageFor(model => Model.Committee.AppointedDate)
</div>
When I click on submit button, the view model which is holding above appointed date is showing DateTime.MinValue
at the controller action method.
Please let us know what's wrong with the above code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是:因为您的输入名称应该是
"Committee.AppointedDate"
但您传递了"CommitteeAppointedDate"
。The short answer is: because your input name should be
"Committee.AppointedDate"
but you pass"CommitteeAppointedDate"
.