【发布时间】:2015-01-01 17:04:41
【问题描述】:
我在我的 mvc 项目的视图中使用 datepicker 来处理两个文本框,即开始日期和结束日期,当我点击提交按钮时,结束日期文本框中的日期不会传递给控制器。它通过默认日期 01/ 01/0001.它在警报中显示日期(“EndDate”);但不传递给控制器。开始日期已过。
$(document).ready(function () {
$('#StartDate').datepicker({
changeMonth: true,
changeYear: true
}).on('changeDate', function (e) {
$(this).datepicker('hide');
});
$('#EndDate').datepicker({
changeMonth: true,
changeYear: true
}).on('changeDate', function (e) {
$(this).datepicker('hide');
//alert($('#EndDate').val());
});
});
function Submit()
{
var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if (EndDate < StartDate) {
alert("Enddate:" + EndDate + " should be greater than Startdate:" + StartDate);
}
else {
//alert(2);
$('#form').submit();
}
}
查看
@using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { @id= "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.PromoCode, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PromoCode)
@Html.ValidationMessageFor(model => model.PromoCode)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" name="StartDate" id="StartDate" />
@Html.ValidationMessageFor(model => model.StartDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" name="EndDate" id="EndDate" />
@Html.ValidationMessageFor(model => model.EndDate)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NewUsers, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NewUsers)
@Html.ValidationMessageFor(model => model.NewUsers)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="button" class="btn btn-primary" onclick="Submit();">Create</button>
@*<input id="submit" type="button" value="Create" class="btn btn-default" />*@
</div>
</div>
</div>
}
控制器
public ActionResult Create(Coupons objCoupons)
{
try
{
//hrllo =nhui
}
catch
{
return View();
}
}
型号
public class Coupons
{
[Required(ErrorMessage = "*")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime EndDate { get; set; }
}
【问题讨论】:
标签: jquery asp.net-mvc-4 datepicker