Asp.NET MVC 2 DataAnnotations UpdateModel <T> Validation
I am trying to use DataAnnotations to add validation to my models in asp.NET MVC 2 RC2 using TryUpdateModel
var user = UserManager.Find(id);
this.TryUpdateModel<IProvisioningObject>(user, form.ToValueProvider());
This updates the model, but validation is never called. I also tried using TryUpdateModel (which is a direct user type) without using a form value provider, using ProvisioningObject directly (which has validation metadata), to no avail.
Google for examples only gives me ways to use DataAnnotations by binding via parameter
public ActionResult Update(User user)
What I don't like about update scripts.
Any tips and / or solutions?
EDIT My objects are objects created automatically using the WCF service.
, DataAnnotations. TryUpdateModel, , -, , , , DataAnnotations. ProvisioningObject, .
[MetadataType(typeof(ProvisioningObjectMetadata))]
public partial class ProvisioningObject : IProvisioningObject
{
public string DisplayNameInvariant { get { return string.IsNullOrEmpty(this.DisplayName) ? this.Name : this.DisplayName; } }
}
[MetadataType(typeof(UserMetadata))]
public partial class User : IUser
{
}
public class ProvisioningObjectMetadata
{
[DisplayName("Country")]
public string CountryIsoCode { get; set; }
[Required(ErrorMessageResourceType = typeof(Properties.Validation), ErrorMessageResourceName = "DisplayNameIsRequired")]
[TempValidator]
public string DisplayName { get; set; }
}
public class UserMetadata
{
[DisplayName("Username")]
public string Name { get; set; }
}
// Controller action
public ActionResult Update(string id, FormCollection form)
{
var user = UserManager.Find(id);
this.TryUpdateModel<IUser>(user.User, form.ToValueProvider());
this.TryUpdateModel<IPerson>(user.User, form.ToValueProvider());
this.TryUpdateModel<IProvisioningObject>(user.User, form.ToValueProvider());
if (ModelState.IsValid) // always true
{
return Redirect;
}
else
{
return View();
}
}
DisplayName UserMetadata, , , . , / , TryUpdateModel .
, , , .
IDataErrorInfo
( )
, . !
: http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-37-cs.aspx