Firstly, this code does work, but it gives me a low level of annoyance that I would like to get rid of. I have a .cshtml page that is built using razor syntax. There is a large, nasty, knockout object living behind the curtains to which many things on the page are tied. Currently, when creating an instance of this knockout model, I do this by serializing the C # model object in question in a string literal, as shown below.
<script type="text/javascript"> $(document).ready(function() { var data = @Html.Raw(Json.Encode(Model)); var contentListingModel = new ContentListingModel(data); ko.applyBindings(contentListingModel, $('#pageRoot').get(0)); }); </script>
As I said, the code works. However, on the line with a call to @ Html.Raw, I get a warning. Visual Studio believes that there is a syntax error (no, after its visualization). Now, this is perfectly reasonable WHY the thing believes that there is a syntax problem. But I would like to get rid of the warning by coding in such a way that it does not cause this problem (I understand that I could hide the warning or do any number of other things), but I just want it not to appear in cases where I serialize C # objects in JSON and type them in javascript on the page. Any ideas?
I completely agree that it is pedantic and petty to ask about it, as the code works fine and seems to be clean, but it annoys me.
source share