ModelState stays valid even if the required attributes are empty in the .NET Framework Web API 4.7.2

Mikhail 41 Reputation points
2023-07-23T13:44:52.31+00:00

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);
            }
        }
    }
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
326 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. QiYou-MSFT 4,321 Reputation points Microsoft Vendor
    2023-07-24T08:14:55.95+00:00

    Hi @Mikhail

    The results I got after my tests were normal. I think the reason may have something to do with the way you sent the request.

    Request1

    Request2

    Best regards,

    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Mikhail 41 Reputation points
    2023-07-26T02:27:07.1+00:00

    I figured this out. I mistakenly referenced the entity from an EF project instead of the model

    After I added a line that would specify what exact model I had to use, it all worked

    using RequestData = QuickbooksDesktopAPI.Models.RequestData;
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.