Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
        }

But when I send empty values for the required fields (I use SOAP UI) and those values come to a controller as NULL values, the ModelState stays valid

What am I doing wrong, and how else can I take advantage of the Required attributes/annotators?

Thank you very much in advance

What I have tried:

I tried to use the filtering functionality using the below code buy no success

public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!actionContext.ModelState.IsValid) {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, actionContext.ModelState);
        }
    }
}
Posted
Updated 25-Jul-23 16:19pm
Comments
Simon_Whale 23-Jul-23 10:13am    
if these are supposed to be nullable, should they also be required?

[Required]
public Nullable<int> RequestType { get; set; }

At a guess, a nullable value can be null/nothing. eg:
VB
[Required]
public Nullable<double> Amount { get; set; }

Change to:
VB
[Required]
public double Amount { get; set; }

Now the null will not be allowed.
 
Share this answer
 
Comments
Member 13304618 23-Jul-23 14:37pm    
Just tried your suggestion. Didn't work
Graeme_Grant 23-Jul-23 17:14pm    
I only picked one as an example. remove Nullable for all and then you may get the desired result.
Member 13304618 23-Jul-23 22:25pm    
I just removed nullable from all - didn't work/help
Graeme_Grant 23-Jul-23 22:40pm    
Is your validation code executing?
Member 13304618 24-Jul-23 7:25am    
Validation code? Where would I check for it?
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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900