我正在将一个模型返回给我关于页面初始加载的视图,模型是从数据库中填充的,我想验证模型,以便当用户收到页面时,验证摘要显示错误(如果有的话)
我尝试过使用TryValidateModel(模型),但是这不起作用,它不会更新ModelState,我认为它只能验证从ModelBinder中填充的内容
这周围有吗?我只是想先验证模型,而无需用户将其发回...
[Authorize, HttpGet, ActionName("StepOne")]
public ActionResult StepOneGET(StepOneModel model)
{
var individual = _onsideService.Get(User.Identity.Name);
model.PersonalInformation = new PersonalInformationModel
{
FirstName = individual.FirstName,
LastName = individual.LastName,
DoB = individual.DateOfBirth.ToString("dd/MM/yyyy"),
Email = individual.DefaultEmail.EmailAddress,
Phone = individual.DefaultPhone.Number,
AddressLine1 = location.Address1,
AddressLine2 = location.Address2,
City = location.City,
PostCode = location.PostalCode,
Country = location.Country
};
// NOTE: Does not update ModelState
TryValidateModel(model);
// Need to return potential errors to user on page load
return View(model);
}
答案 0 :(得分:2)