模型验证(1)
Model-level Validation
1.添加类
public class Appointment {
public string ClientName { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
public bool TermsAccepted { get; set; }
}
2.添加接口
public interface IAppointmentRepository {
void SaveAppointment(Appointment app);
}
3.实现接口
public class DummyAppointmentRepository : IAppointmentRepository {
public void SaveAppointment(Appointment app) {
// do nothing
}
}
4.依赖注入
public class NinjectDependencyResolver : IDependencyResolver {
private IKernel kernel;
public NinjectDependencyResolver() {
kernel = new StandardKernel();
AddBindings();
}
public object GetService(Type serviceType) {
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType) {
return kernel.GetAll(serviceType);
}
public IBindingToSyntax<T> Bind<T>() {
return kernel.Bind<T>();
}
public IKernel Kernel {
get { return kernel; }
}
private void AddBindings() {
Bind<IAppointmentRepository>().To<DummyAppointmentRepository>();
}
}
5.添加控制器
public class AppointmentController : Controller
{
private IAppointmentRepository repository;
public AppointmentController(IAppointmentRepository repo)
{
repository = repo;
}
public ViewResult MakeBooking()
{
return View(new Appointment { Date = DateTime.Now });
}
[HttpPost]
public ViewResult MakeBooking(Appointment appt)
{
if (string.IsNullOrEmpty(appt.ClientName))
{
ModelState.AddModelError("ClientName", "Please enter your name");
}
if (ModelState.IsValidField("Date") && DateTime.Now > appt.Date)
{
ModelState.AddModelError("Date", "Please enter a date in the future");
}
if (!appt.TermsAccepted)
{
ModelState.AddModelError("TermsAccepted", "You must accept the terms");
}
if (ModelState.IsValidField("ClientName") && ModelState.IsValidField("Date")
&& appt.ClientName == "Joe" && appt.Date.DayOfWeek == DayOfWeek.Monday)
{
ModelState.AddModelError("", "Joe cannot book appointments on Mondays");
}
if (ModelState.IsValid)
{
repository.SaveAppointment(appt);
return View("Completed", appt);
}
else
{
return View();
}
}
}
6.添加视图MakeBooking.cshtml
@model MvcApp.Models.Appointment
@{
ViewBag.Title = "Make A Booking";
}
<h4>Book an Appointment</h4>
@using (Html.BeginForm()) {
@Html.ValidationSummary()
<p>Your name: @Html.EditorFor(m => m.ClientName)</p>
<p>Appointment Date: @Html.EditorFor(m => m.Date)</p>
<p>@Html.EditorFor(m => m.TermsAccepted) I accept the terms & conditions</p>
<input type="submit" value="Make Booking" />
}
7.添加视图Complete.cshtml
@model MvcApp.Models.Appointment
@{
ViewBag.Title = "Confirmed";
}
<h4>Your appointment is confirmed.</h4>
<p>Your name is: @Html.DisplayFor(m => m.ClientName)</p>
<p>The date of your appontment is: @Html.DisplayFor(m => m.Date)</p>
效果

Property-Level Validation
修改视图MakeBooking.cshtml如下
@model MvcApp.Models.Appointment
@{
ViewBag.Title = "Make A Booking";
}
<h4>
Book an Appointment</h4>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<p>
Your name: @Html.EditorFor(m => m.ClientName)
@Html.ValidationMessageFor(m => m.ClientName)
</p>
<p>
Appointment Date: @Html.EditorFor(m => m.Date)
@Html.ValidationMessageFor(m => m.Date)
</p>
<p>@Html.EditorFor(m => m.TermsAccepted) I accept the terms & conditions
@Html.ValidationMessageFor(m => m.TermsAccepted)
</p>
<input type="submit" value="Make Booking" />
}
效果


浙公网安备 33010602011771号