日期未从View传递到Controller

时间:2018-07-30 07:23:43

标签: asp.net-mvc

我目前在我的视图中有一个日期选择器,其中保存了用户选择日期的日期。之后,我需要将日期传递给控制器​​,但是到达控制器时该日期为空。我不确定我是否以错误的方式进行处理。下面是我刚才解释的代码。

控制器:

public ActionResult Index()
    {
        ViewBag.location_id = new SelectList(db.Locations, "location_id", "location_name");
        return View();
    }

    [HttpPost]
    public ActionResult Index(DatePickerModel PMV)
    {
        TempData["dti"] = PMV.dtmDateIn.ToString("dd/mm/yy");
        TempData["dto"] = PMV.dtmDateOut.ToString("dd/mm/yy");

        return RedirectToAction("reservation_step_1", "Home");
    }

所以我基本上是在尝试从索引视图返回值,如下所示:

@model FleetHire.Models.DatePickerModel    
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
   {
       @Html.DropDownList("locationId", ViewBag.location_id as SelectList, "Select Location", htmlAttributes: new { @class = "form-control" })

         @Html.EditorFor(model => model.dtmDateOut, new { htmlAttributes = new { @class = "datepicker", @style = "height: 30px" } })

          @Html.EditorFor(model => model.dtmDateIn, new { htmlAttributes = new { @class = "datepicker", @style = "height: 30px" } })

           <input type="submit" style="background-color:#DDDDDD; width: 139.5px; height: 30px;" value="SEARCH" onclick="@("window.location.href='" + @Url.Action("reservation_step_1", "Home") + "'");"/>

<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
@section scripts
{
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
    <script>
        $(function ()
        {
            $(".datepicker").datepicker({
                dateFormat: "dd/mm/yy",
                changeMonth: true,
                changeYear: true,
                yearRange: "+0:+1",
                minDate: "+0",
                showOn: "both",
                buttonText: "<i class='fa fa-calendar' style='height: 25px; margin-top: 6px;margin-bottom: -4px; border-style: none;'></i>"
            });
        });
    </script>
}
</div>
   }

接收到这些值之后,我将这些值传输到控制器中的“ reservation_step_1()”操作中,以计算出日期之间的差异。如下:

 [HttpGet]
    public ActionResult reservation_step_1()
    {
        DateTime O = DateTime.ParseExact(TempData["dto"].ToString().Trim(), "dd/mm/yy", CultureInfo.InvariantCulture);
        DateTime I = DateTime.ParseExact(TempData["dti"].ToString().Trim(), "dd/mm/yy", CultureInfo.InvariantCulture);

        var days = I.Subtract(O).Days.ToString();

        ViewBag.totdays = days;
        return View(db.Step_1.ToList());
    }

为“ reservation_step_1()”操作加载视图后,ViewBag.totdays值为0。

有人可以帮我解决这个问题吗?我不知道什么地方或哪里出了错

运行时出现错误:

Values of O and I

Error

1 个答案:

答案 0 :(得分:0)

AFAIK,ASP.NET MVC TempData可用于在控制器动作之间共享数据。对我来说,我更喜欢按如下方式传递强类型对象:

public ActionResult reservation_step_1(string dto,string dti)
{
   //...
}

public ActionResult Index(DatePickerModel PMV)
{
    string dti = PMV.dtmDateIn.ToString("dd/mm/yy");
    string dto = PMV.dtmDateOut.ToString("dd/mm/yy");
    return RedirectToAction("reservation_step_1", "Home", new { dto = dto,dti = dti});
}