I have a strange situation, and none of the existing solutions help
I have this model in the .NET Framework Web API
public partial class RequestData
{
public int Id { get; set; }
[Required(AllowEmptyStrings = false)]
public string AccountName { get; set; }
[Required(AllowEmptyStrings = false)]
public string VendorName { get; set; }
[Required]
public string Memo { get; set; }
public Nullable<bool> IsToBePrinted { get; set; }
[Required]
public string AccountType { get; set; }
[Required]
public Nullable<double> Amount { get; set; }
public Nullable<bool> IsProcessed { get; set; }
public string Ticket { get; set; }
public string Response { get; set; }
public Nullable<System.DateTime> Updated { get; set; }
[Required]
public Nullable<int> RequestType { get; set; }
[Required]
public string TxnId { get; set; }
[Required]
public string EditSequence { get; set; }
[Required]
public string ListID { get; set; }
[Required]
public string TxnLineId { get; set; }
}
I have a POST controller that uses this model
[HttpPost]
public ActionResult PostNewRequest([FromBody] RequestData requestData)
{
try
{
if (!ModelState.IsValid) {
//Your code if the field is invalid
} else {
//Your code if the field is valid
}
I also tried to use the filtering functionality but it is not working
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false) {
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}