I have a class called 'User' and a property 'Name'
public class User
{
[Required]
public string Name { get; set; }
}
And api controller method is
public IHttpActionResult PostUser()
{
User u = new User();
u.Name = null;
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok(u);
}
How do i manually validate the User object so the ModelState.IsValid return false to me?
Userobject manually? If it was the model, the user would be an input parameter to thePostUsermethod. Do you want to know how to validate an arbitrary object against the rules specified by data annotations (e.g.[Required])?