datepicker将null值传递给controller

时间:2014-11-05 13:23:26

标签: jquery asp.net-mvc-4 datepicker

我在我的mvc项目视图中使用datepicker两个文本框即开始日期和结束日期,当我点击提交按钮时,我在结束日期文本框中的日期不会传递给controller.It传递默认日期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; }
}

1 个答案:

答案 0 :(得分:0)

我无法复制您的问题,是否可以向我们展示您的所有视图代码?

我也不知道你是否把你的日期放在BeginForm

之内
       @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "Createform" }))
      {
             <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.EditorFor(model => model.StartDate, new { @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 data-format="MM-dd-YYYY" type="text" name="EndDate" id="EndDate" />
        @*@Html.EditorFor(model => model.EndDate, new { @id = "EndDate" })*@
        @Html.ValidationMessageFor(model => model.EndDate)
    </div>
</div>

        <div class="three-column-right">
            <input type="button" onclick="Submit();" class="nar-btn nar-form-size75" value="Göster" />
        </div>
     }

<script type="text/javascript">

  $(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 {
        $('#Createform').submit();
    }
}

</script>