Click here to Skip to main content
15,921,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I have created ASP.NET Web API and calling the post method using json from client side and it's working fine :



C#
public class ValueController : ApiController
{
    public void Post([FromBody]model value)
    {

    }
}

public class model
{
    public decimal value { get; set; }
}



C#
string JSONString = "{\"value\":\"999.99\"}";


C#
but when I use the [Required] attribute in the value property, 



C#
public class model
{
    [Required]
    public decimal value { get; set; }
}



start giving protocol Error

What I have tried:

Tried to change the format of JSON string
like:
string JSONString = "{\"value\":999.99}";

but did not work it...
Posted
Updated 5-Jul-16 0:17am

1 solution

C#
Since decimal is a Value type and if we use the Required attribute to a value type, it will cause an error.. it will always have a value (the default of 0) if the incoming request does not provide the value.

Now I am using [DataMember(isRequired=true)] instead of [Required] attribute
 
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