public ActionResult Index() { Address address = new Address { Province = "江苏", City = "苏州", District = "工业园区", Street = "星湖街328号" }; Contact contact = new Contact { Name = "张三", PhoneNo = "123456789", EmailAddress = "*****@*****.**", Address = address }; return View(contact); }
public ActionResult Index() { Address address = new Address { Province = "江苏", City = "苏州", District = "工业园区", Street = "星湖街328号" }; Contact contact = new Contact { Name = "张三", PhoneNo = "123456789", EmailAddress = "*****@*****.**", Address = address }; ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => contact, typeof(Contact)); ModelValidator validator = ModelValidator.GetModelValidator(metadata,ControllerContext); return View(validator.Validate(contact)); }