Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 08, 2014 @ 15:22
    Jeroen Breuer
    0

    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; }
    }

    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

    Jan 08, 2014 @ 15:30

    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 :)

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jan 08, 2014 @ 15:34
    Anders Bjerner
    1

    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.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 08, 2014 @ 15:36
    Jeroen Breuer
    0

    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

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Jan 08, 2014 @ 15:41
    Anders Bjerner
    0

    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:

    new MaterialItem()
    {  
        Code = x.Code,
        Name = x.Name,
        Price = x.Price.ToString(dutchCultureInfo)
    }
    

    That would give a JSON string like this:

    {"Code":1234,"Name":"Some name","Price":"9,66"}
    
  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 08, 2014 @ 15:47
    Jeroen Breuer
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 08, 2014 @ 16:02
    Jeroen Breuer
    0

    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

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Jan 08, 2014 @ 16:12
  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Jan 08, 2014 @ 16:27
    Damiaan
    0

    Can't you change the actionresult to jsonresult?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 08, 2014 @ 16:42
    Jeroen Breuer
    0

    @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:

    (bd.Price / 100).ToString("C", culture)

    Jeroen

  • neetu 1 post 71 karma points
    Jul 06, 2016 @ 05:46
    neetu
    0

    Hello Jeoren,

    Did you solve this issue ? If yes then please let me know how?

Please Sign in or register to post replies

Write your reply to:

Draft