将日期值从视图传递到控制器

时间:2018-05-08 17:54:23

标签: asp.net-mvc

在我的模特中我有

[DataType(DataType.Date)]
    public DateTime SaleDate { get; set; }
    public virtual Customer Customers { get; set; }

在我的控制器中:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include="SaleID,shippingBillNo,CustomerID,PlantID,Quantity,SaleDate")] Sale sale)
    {
        if (ModelState.IsValid)
        {
            var strddlValue = Convert.ToInt32(Request.Form["CustomerID"]);
            var saledate = Convert.ToDateTime(Request.Form["mySaleDate"]);

在我的视图中

@using (Html.BeginForm(null, null, FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Sale</h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.LabelFor(model => model.CustomerID, "CustomerID", new { 
        @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.Hidden("CustomerText")
                @Html.DropDownList("CustomerID", String.Empty)
                @Html.ValidationMessageFor(model => model.CustomerID)
            </div>
        </div>
                    <div class="form-group">
            @Html.LabelFor(model => model.SaleDate, new { @class = "control- 
                label col-md-2" })   
            <div class="col-md-10">
                @Html.EditorFor(model => model.SaleDate, new { Name = 
                 "mySaleDate"})
                @Html.ValidationMessageFor(model => model.SaleDate)
            </div>
        </div>

和DateTimePicker的JavaScript是:

<script type="text/javascript">
$(function () {
    $('#SaleDate').datetimepicker({
        format: "DD/MM/YYYY"
    }).on('dp.change', function (e) {
        $(this).data('DateTimePicker').hide();
    });
});

我成功将CustomerID传递给控制器​​。没问题。我没有使用DataTimePicker帮助器传递我在editofor中选择的日期,而是获得了无意义的值'01 / 01/0001'。我做错了什么?

0 个答案:

没有答案