开发者

How can I have Raw HTML inside a JsonConvert's SerializeObject()?

开发者 https://www.devze.com 2023-04-04 21:50 出处:网络
I\'m trying to recreate the following JavaScript code using Newtonsoft\'s parser: var nav = { container: $(\'.ux-navigation-control\'), manual: true, validate: true };

I'm trying to recreate the following JavaScript code using Newtonsoft's parser:

var nav = { container: $('.ux-navigation-control'), manual: true, validate: true };

Trying to use Html.Raw inside Newtonsoft like:

var nav = @(new HtmlString(JsonConvert.SerializeObject(new
                                                      {
                                                          container = Html.Raw("$('.ux-navigation-control')"),
                                                          manual = true,
                                                          validate = true
                                                      }))) ;

Returns an empty object inste开发者_如何学Goad of the desired expression:

var nav = {"container":{},"manual":true,"validate":true} ;

Any help?


$('.ux-navigation-control') isn't valid JSON, so most if not all JSON parsers will throw this out. You should just return the selector instead and do some post-processing on the client side, like so:

$.getJSON('/myurl', function(nav) {
  nav.container = $(nav.container);
  // do something else with nav
});
0

精彩评论

暂无评论...
验证码 换一张
取 消