Reputation: 2878
I have a simple WebApi 2.1 project and am receiving a model state error when I post and leave the value for a non-required nullable field (double type) blank. I want to leave the xml element but allow it to be blank. I had assumed making the field nullable would handle this but instead, if I post with salary left blank, I receive the error (listed below). If I put a number in... such as <salary>32000</salary>
everything works as expected.
How can I allow a blank value for a nullable double and still pass model validation?
The error I receive:
System.Web.Http.ModelBinding.ModelError: "There is an error in XML document (1, 111)."
My Model:
public class Employee
{
[Required]
public string LastName { get; set; }
public string MiddleName { get; set; }
[Required]
public string FirstName { get; set; }
public double? Salary { get; set; }
}
My Controller Post Method:
public HttpResponseMessage Post([FromBody]Employee employee)
{
if (ModelState.IsValid)
{
string firstName = employee.FirstName;
string lastName = employee.LastName;
if (employee.Salary != null)
{
double? salary = employee.Salary;
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NoContent, ModelState);
}
}
Here is my actual Post (with headers listed):
Content-Type: application/xml
Accept-Language: en-us
Accept: application/xml
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LastName>Charlie</LastName>
<MiddleName>Bravo</MiddleName>
<FirstName>Alpha</FirstName>
<Salary></Salary>
</Employee>
Upvotes: 1
Views: 1746
Reputation: 7484
If you have to handle an empty Salary node (as opposed to a missing one), I'd change the model to the following:
public class Employee
{
[Required]
public string LastName { get; set; }
public string MiddleName { get; set; }
[Required]
public string FirstName { get; set; }
public string Salary { get; set; }
public double? SalaryAmount
{ get
{
if (string.IsNullOrEmpty(Salary)) return null;
return double.parse(Salary);
}
]
}
Disclaimer: I don't have VS handy so I wasn't able to validate my code.
You then have to use SalaryAmount when you are consuming the Employe instance. The reason for this is an empty XML element evaluates to string.Empty; a missing XML element evaluates to null. Double.Parse does not like empty strings which is what is causing your problem.
One other thing I'd recommend is to not use floating point when dealing with money; it introduces rounding and other problems. I'd change double to a decimal.
Upvotes: 2
Reputation: 17485
I suggest please don't include salary node in XML if it is blank. Try it and it should work then.
Try this : user xsi:nil="true"
Content-Type: application/xml
Accept-Language: en-us
Accept: application/xml
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LastName>Charlie</LastName>
<MiddleName>Bravo</MiddleName>
<FirstName>Alpha</FirstName>
<Salary xsi:nil="true"></Salary>
</Employee>
Upvotes: 1