FYI,我还使用jQuery从部分页面填充了一个字段model.VdiId。当我单击表单页面的提交按钮时,它会将我重定向到空白页面,而不是单击“预订”控制器的BookVdi操作。我抛出异常并设置了没有达到的断点。
下面是查看代码:
@model HVDI.Models.BookingViewModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (@Html.BeginForm("BookVdi", "Booking", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<h4>Booking</h4>
<hr />
<div class="form-inline">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.VdiId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.VdiId, new { htmlAttributes = new { @class = "form-control disabled" } })
@Html.ValidationMessageFor(model => model.VdiId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.BookingDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BookingDate, new { htmlAttributes = new { @class = "form-control datepicker" } })
@Html.ValidationMessageFor(model => model.BookingDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TimeStart, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TimeStart, new { htmlAttributes = new { @class = "form-control timepicker" } })
@Html.ValidationMessageFor(model => model.TimeStart, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TimeEnd, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TimeEnd, new { htmlAttributes = new { @class = "form-control timepicker" } })
@Html.ValidationMessageFor(model => model.TimeEnd, "", new { @class = "text-danger" })
</div>
</div>
<div class="showing">
<input type="button" class="align-content-end" onclick="" id="btnSearch" value="Search" />
</div>
</div>
<br />
<div class="form-group">
<div class="table-dark" id="vdiSection">
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Book" class="btn btn-default" />
</div>
</div>
}
下面是控制器代码:
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public void BookVdi(BookingViewModel bookingDetails)
{
if (!ModelState.IsValid)
{
throw new Exception();
}
Booking newBooking = new Booking();
newBooking.BookingDate = bookingDetails.BookingDate; //I am putting a breakpoint here
}
您看到空白页,因为正确执行操作可能不会返回任何内容。请根据以下内容更改代码。
[HttpPost]
[ValidateAntiForgeryToken]
public void BookVdi(BookingViewModel bookingDetails)
{
if (!ModelState.IsValid)
{
ViewBag,Error = "Please, correct all errors.";
return View("YOURVIEWNAMEFOR FORM");
}
Booking newBooking = new Booking();
newBooking.BookingDate = bookingDetails.BookingDate;
// Write save code here
return RedirectToAction("Index"); // you must redirect to something when action executed.
}
也添加此ViewBag以查看视图错误
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@ViewBag.Error // add this line
<h4>Booking</h4>
也要查看此问题是否需要在视图question中显示特定的模型错误