Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding JSON from MVC view with lowercase keys?

I have the following in my MVC view:

 $(document).ready(ko.applyBindings(new ProfileVm(@Html.Raw(Json.Encode(Model)))));

This is working great, except all of the keys from Model are being encoded with uppercase first letters. How can I invoke the camelCase resolver from the view? Or is there a way to tell Json.Encode to use lowercase resolution?

like image 312
RobVious Avatar asked Mar 10 '26 18:03

RobVious


1 Answers

You should use Newsoft JSON for this:

@{ var json = JsonConvert.SerializeObject(
            Model, 
            Formatting.None, 
            new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
   );

    $(document).ready(ko.applyBindings(new ProfileVm(@Html.Raw(Json.Encode(json)))));
}
like image 133
Fals Avatar answered Mar 13 '26 16:03

Fals



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!