我正在尝试使用“必需”数据注释,因此用户需要填写客户端字段。尽管如果用户将该字段留空并提交表单,它会抛出错误,而不是告诉用户客户端“需要开始时间”。
错误:
我的数据注释:
[Required(ErrorMessage = "A start time is required")]
public string StartTime { get; set; }
[Required(ErrorMessage = "An end time is required")]
public string EndTime { get; set; }
我的表单的 Controller :
[HttpGet]
public ActionResult BookingForm(School model)
{
School school = db.Schools.First(m => m.RollNumber ==
model.RollNumber);
return View(school);
}
[HttpPost]
[ActionName("BookingForm")]
public ActionResult BookingFormPost(Booking model)
{
db.Bookings.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
查看:
<form asp-controller="School" asp-action="BookingForm" method="post">
<br />
<p>Please complete the following information</p>
<div>
<label for="RollNumber">School Roll Number:</label>
<br />
<input asp-for="Booking.Rollnumber" id="txtRoll" name="RollNumber"
value="@Model.RollNumber" />
</div>
<br />
<div>
<label for="Date">Choose Camp Date:</label>
<br />
<input asp-for="Booking.Date" id="txtDate" name="Date"
value="@Model.Date.Value.ToShortDateString()" />
</div>
<br />
<div>
<label for="OfficialSchoolName">School Name:</label>
<br />
<input asp-for="Booking.OfficialSchoolName" id="txtOfficialSchoolName"
name="OfficialSchoolName" value="@Model.OfficialSchoolName" />
</div>
<br />
<div>
<label for="Address">School Address:</label>
<br />
<input asp-for="Booking.Address1" id="txtAddress1" name="Address1"
value="@Model.Address1" />
<br />
<input asp-for="Booking.Address2" id="txtAddress2" name="Address2"
value="@Model.Address2" />
<br />
<input asp-for="Booking.Address3" id="txtAddress3" name="Address3"
value="@Model.Address3" />
<br />
<input asp-for="Booking.Address4" id="txtAddress4" name="Address4"
value="@Model.Address4" />
</div>
<br />
<div>
<label for="Times">Times Needed:</label>
<br />
<input asp-for="Booking.StartTime" id="txtStartTime" name="StartTime"
value="Start Time e.g. 10:30" />
<input asp-for="Booking.EndTime" id="txtEndTime" name="EndTime"
value="End Time e.g. 16:00" />
</div>
<br />
<div>
<label for="ClassGroups">Class Groups:</label>
<br />
<input asp-for="Booking.ClassGroups" id="txtClassGroups"
name="ClassGroups" value="e.g. 4th class, 6th class, 1st year etc." />
</div>
<br />
<div>
<label for="Topics">Topics to be covered:</label>
<br />
<input asp-for="Booking.Topics" id="txtTopics" name="Topics" value="" />
</div>
<br />
<div>
<label for="TeacherName">Teacher Name:</label>
<br />
<input asp-for="Booking.TeacherName" id="txtTeacherName"
name="TeacherName" />
</div>
<br />
<div>
<label for="Email">Teacher Email:</label>
<br />
<input asp-for="Booking.Email" id="txtEmail" name="Email" />
</div>
<br />
<div>
<label for="PhoneNumber">Teacher Contact Number:</label>
<br />
<input asp-for="Booking.PhoneNumber" " id="txtPhoneNumber"
name="PhoneNumber" value="@Model.PhoneNumber" />
</div>
<br />
<div>
<label for="Surveys">Would you like to take part in research surveys?
</label>
<br />
<input asp-for="Booking.Surveys" id="Surveys" name="Surveys"
type="checkbox" checked />
</div>
<br />
<input type="submit" value="Submit" class="submit" />
</form>
谁能告诉我为什么客户端验证没有发生?为什么我会收到错误?
干杯
编辑:
我尝试了以下 Controller 更改
[HttpGet]
public ActionResult BookingForm(School model)
{
School school = db.Schools.First(m => m.RollNumber ==
model.RollNumber);
return View(school);
}
[HttpPost]
[ActionName("BookingForm")]
public ActionResult BookingFormPost(Booking model)
{
if (ModelState.IsValid)
{
db.Bookings.Add(model);
db.SaveChanges();
return RedirectToAction("Index","Home");
}
return View(model);
}
当我提交表单时,我会收到以下浏览器消息:
“/”应用程序中的服务器错误。 传递到字典中的模型项的类型为“CampBookingSys.Models.Booking”,但此字典需要类型为“CampBookingSys.Models.School”的模型项。
最佳答案
在接收模型及其属性上的数据注释的 Controller 操作上,您应该有类似的内容
public IActionResult Get(Obj blah)
{
if (ModelState.IsValid)
{
...
return View("WhateverPageIsAfterSuccess")
}
return RedirectToAction("IndexAction")
}
关于c# - 所需的数据注释抛出错误而不是验证客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581088/