SurfaceController ActionResult returns wrong JSON format
Hello,
I've got a SurfaceController with an ActionResult that returns JSON, but the JSON is in the wrong format. I know I could also use the WebAPI to return JSON, but that is stateless and I'm using a session in this method.
Here is my ActionResult:
public class JsonController : SurfaceController
{
/// <summary>
/// Return the materials
/// </summary>
/// <returns></returns>
public ActionResult GetOrderMaterials()
{
var materials = MaterialLogic.GetMaterialItems();
return Json(
materials.Select(x =>
new MaterialItem()
{
Code = x.Code,
Name = x.Name,
Price = x.Price
}
),
JsonRequestBehavior.AllowGet
);
}
}
This is the MaterialItem object:
public class MaterialItem
{
public string Code { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
The decimal value of Price is 9.66, but when I view the JSON which is returned it becomes 9.6600. The problem is that this is a dutch website and 9.66 should become 9,66 in the JSON. I already tried changing the culture in the ActionResult, but that doesn't help. I could convert it to a string first before returning it as JSON, but that doesn't feel like a good solution. The model is also used for model binding somewhere else so it's better to keep it a decimal.
Maybe it's not a problem and the value in JSON should be 9.6600 because it's still a decimal and can be converted to 9,66 later in the code, but I'm not sure about that.
If your JSON returns the price as a decimal number rather than a string, something like {"Price":9,66} would not considered valid, so the behavior seems correct.
The fix would be to either send the price as a string, or as you suggests yourself - convert the value later in the code.
I realized that it should be a decimal in the JSON format so I'm not going to change my code. I already suggested I could convert it to a string first before returning it as JSON, but that doesn't feel like a good solution.
In the end there isn't really a problem and I thought that the decimal in JSON could be 9,66, but that's not possible.
A bit off topic, but I'm now having trouble with model binding back to my controller.
I've got a hidden field with the following decimal (which I got from the JSON).
395434.64
If I post this to my controller and check the decimal the value is
39543464
So the . is missing and the value becomes 100x greater.
If my hidden field has a comma it does work.
395434,64
If I post this to my controller and check the decimal the value is
395434.64
So that's probably a culture problem, but how can I solve that? I already looked at Model Binding Decimal Values, but that didn't fix it. The culture of my website is nl-NL. There must be more people who've had this problem ;-).
@Dmiaan The ActionResult is already a JsonResult because my code says:
return Json(
@Sebastiaan I'm now using decimal and cents and that works. I'm still using decimals because I need to display the money in an email so I devide it again:
SurfaceController ActionResult returns wrong JSON format
Hello,
I've got a SurfaceController with an ActionResult that returns JSON, but the JSON is in the wrong format. I know I could also use the WebAPI to return JSON, but that is stateless and I'm using a session in this method.
Here is my ActionResult:
This is the MaterialItem object:
This is the url I use:
project.local/umbraco/surface/json/GetOrderMaterials
The decimal value of Price is 9.66, but when I view the JSON which is returned it becomes 9.6600. The problem is that this is a dutch website and 9.66 should become 9,66 in the JSON. I already tried changing the culture in the ActionResult, but that doesn't help. I could convert it to a string first before returning it as JSON, but that doesn't feel like a good solution. The model is also used for model binding somewhere else so it's better to keep it a decimal.
Maybe it's not a problem and the value in JSON should be 9.6600 because it's still a decimal and can be converted to 9,66 later in the code, but I'm not sure about that.
Jeroen
Comment author was deleted
I'd just format it in the view since math needs a decimal in the backend.
I think it makes sense to leave the model in-tact just as you would a date that is formatted for different regions (USA vs UK).
I hope I understood what you're asking since I didn't see a question in there :)
Hi Jeroen,
If your JSON returns the price as a decimal number rather than a string, something like {"Price":9,66} would not considered valid, so the behavior seems correct.
The fix would be to either send the price as a string, or as you suggests yourself - convert the value later in the code.
I was wondering if I could do something to return the decimal in JSON as 9,66 because that's the dutch value.
A decimal always should be 9.66 probably so there is probably nothing I can do about my ActionResult. Thanks for the feedback.
Jeroen
It depends on what the JSON should be used for. Something like the code below might work if you just need to display the price:
That would give a JSON string like this:
I realized that it should be a decimal in the JSON format so I'm not going to change my code. I already suggested I could convert it to a string first before returning it as JSON, but that doesn't feel like a good solution.
In the end there isn't really a problem and I thought that the decimal in JSON could be 9,66, but that's not possible.
Jeroen
A bit off topic, but I'm now having trouble with model binding back to my controller.
I've got a hidden field with the following decimal (which I got from the JSON).
395434.64
If I post this to my controller and check the decimal the value is
39543464
So the . is missing and the value becomes 100x greater.
If my hidden field has a comma it does work.
395434,64
If I post this to my controller and check the decimal the value is
395434.64
So that's probably a culture problem, but how can I solve that? I already looked at Model Binding Decimal Values, but that didn't fix it. The culture of my website is nl-NL. There must be more people who've had this problem ;-).
Jeroen
"It is often recommended to handle money as an integer representing the number of cents"
Can't you change the actionresult to jsonresult?
@Dmiaan The ActionResult is already a JsonResult because my code says:
@Sebastiaan I'm now using decimal and cents and that works. I'm still using decimals because I need to display the money in an email so I devide it again:
Jeroen
Hello Jeoren,
Did you solve this issue ? If yes then please let me know how?
is working on a reply...