6

Closed

Provide support for specifying required action parameter from body

description

Currently the [Required] attribute can be used on properties but not on the type itself or on the parameter. So when sending a request with empty body to the POST action below, the ModelState.IsValid would return true even though everything is missing. Right now we don't have good ways of specifying a required body parameter.

public POST(LoginModel login)
{
if (ModelState.IsValid)
{
    // do something with the login parameter
}
}

public class LoginModel
{
[Required]
public string UserName { get; set; }

[Required]
public string Password { get; set; }
}
Closed Feb 23 at 2:03 AM by HongmeiG
This is a dup of issue 24.

comments

michaelstum wrote Sep 27, 2012 at 9:04 PM

Upvoting this because it's weird and unexpected. LoginModel will be NULL in the above example, bit ModelState.IsValid will always be true.

aforty wrote Jan 15 at 5:05 PM

I can't seem to get any validation using System.ComponentModel.DataAnnotations to works. In fact, I can't get ModelState.IsValid to ever be false even if the object I construct is complete garbage.