我的家庭控制器的“索引”视图中有2个日期选择器。我正在尝试将从视图中的datepickers(日期输入和日期输出)中选择的数据传递给控制器中的另一个操作(reservation_step_1())。
查看代码:
@using (Html.BeginForm())
{
@Html.DropDownList("location_id", null, htmlAttributes: new { @class = "form-control" })
}
</div>
<div style="float:left">
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.DatePickerModel.dtmDateOut, new { htmlAttributes = new { @class = "datepicker", @style = "height: 30px" } })
}
</div>
<div class="styled-select" style="float:left; padding-bottom: 10px">
@Html.DropDownList("PickUpTime", new List<SelectListItem>
{
new SelectListItem {Text="08:00",Value="08:00"},
new SelectListItem {Text="09:00",Value="09:00"},
new SelectListItem {Text="10:00",Value="10:00"},
new SelectListItem {Text="11:00",Value="11:00"},
new SelectListItem {Text="12:00",Value="12:00"},
new SelectListItem {Text="13:00",Value="13:00"},
new SelectListItem {Text="14:00",Value="14:00"},
new SelectListItem {Text="15:00",Value="15:00"},
new SelectListItem {Text="16:00",Value="16:00"},
new SelectListItem {Text="17:00",Value="17:00"}
}, new { @style = "height: 30px" })
</div>
<div style="background-color: #dddddd; height: 30px; width: 30px; float:left">
<p style="font-size: 15px; text-align:center; padding-top: 3px">TO</p>
</div>
<div style="float:left">
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.DatePickerModel.dtmDateIn, new { htmlAttributes = new { @class = "datepicker", @style = "height: 30px" } })
}
</div>
<div class="styled-select" style="float:left; padding-bottom: 10px;">
@Html.DropDownList("DropPffTime", new List<SelectListItem>
{
new SelectListItem {Text="08:00",Value="08:00"},
new SelectListItem {Text="09:00",Value="09:00"},
new SelectListItem {Text="10:00",Value="10:00"},
new SelectListItem {Text="11:00",Value="11:00"},
new SelectListItem {Text="12:00",Value="12:00"},
new SelectListItem {Text="13:00",Value="13:00"},
new SelectListItem {Text="14:00",Value="14:00"},
new SelectListItem {Text="15:00",Value="15:00"},
new SelectListItem {Text="16:00",Value="16:00"},
new SelectListItem {Text="17:00",Value="17:00"}
}, new { @style = "height: 32px;" })
</div>
<div style="float:left">
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<input type="submit" style="background-color:#DDDDDD; width: 139.5px; height: 30px;" value="SEARCH" onclick="@("window.location.href='" + @Url.Action("reservation_step_1", "Home") + "'");"/>
}
</div>
</div>
<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>
}
控制器中的代码
using FleetHire.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Hosting;
using System.Globalization;
namespace FleetHire.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Index()
{
ViewBag.location_id = new SelectList(db.Locations, "location_id", "location_name");
return View();
}
[HttpPost]
public ActionResult Index(DateTime dtIn, DateTime dtOut)
{
TempData["dto"] = dtOut;
TempData["dti"] = dtIn;
return RedirectToAction("reservation_step_1", "Home");
}
[HttpGet]
public ActionResult reservation_step_1()
{
string d_out = TempData["dto"].ToString();
string d_in = TempData["dti"].ToString();
DateTime O = DateTime.ParseExact(d_out, "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime I = DateTime.ParseExact(d_in, "dd/MM/yyyy", CultureInfo.InvariantCulture);
var days = I.Subtract(O).Days.ToString();
ViewBag.totdays = days;
return View(db.Step_1.ToList());
}
}
}
我正在获取NullReferenceException(“对象引用未设置为对象的实例。”)导致此错误的行是:
string d_out = TempData["dto"].ToString();
string d_in = TempData["dti"].ToString();
在reservation_step_1操作中,我试图从Index操作中获取日期和日期,并获取从过期到日期的天。
是什么原因导致此问题或我在做什么错?
非常感谢您的帮助!
以下建议似乎可行,但是我现在遇到了DateTime转换问题
下面的行引发以下错误(未将字符串识别为有效的DateTime。)
DateTime.ParseExact(d_out, "dd/MM/yyyy", CultureInfo.InvariantCulture);
答案 0 :(得分:0)
将所有相同的DateTimePicker元素放在同一BeginForm上。我建议您删除此内容:
@using (Html.BeginForm())
{
@Html.EditorFor(model => model.DatePickerModel.dtmDateOut, new { htmlAttributes = new { @class = "datepicker", @style = "height: 30px" } })
}
仅此一个:
@Html.EditorFor(model => model.DatePickerModel.dtmDateOut, new { htmlAttributes = new { @class = "datepicker", @style = "height: 30px" } })
由于您的代码开头已经有Html.BeginForm。在另一个DateTimePicker上执行此操作。
在代码的开头执行以下操作:
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
//Put all your logics here. Its not necessary to put any BeginForm on other elements.
}
正如评论中的Stephen Muecke所述,将视图中的元素名称/ id绑定到将在Controller上传递的Model:
[HttpPost]
public ActionResult Index(DatePickerModel objModel)
{
TempData["dto"] = objModel.dtOut;
TempData["dti"] = objModel.dtIn;
}