Friday, March 14, 2014

Knockout and Razor syntax awkwardness

 

While working on a project in ASP.NET MVC 5 with Razor and Knockout, I came across a little awkwardness in the syntax highlighting.
Take a look at the following piece of code:

   1:  <label for="Income" data-bind="text : formatLabel('@Translations.ReviewDetails_Income',[IncomeYear])"></label>
note: Translations is a resx file with our translations in it… obviously.awkward syntax

The syntax highlighting is confused by the fact that there is an @ in there. While this syntax is perfectly valid and the view does compile and display’s correctly.
The solution is actually very simple, but isn’t really the way you want to use this type of syntax.


   1:  <label for="Income" data-bind="@string.Format("text : formatLabel('{0}',[IncomeYear])",Translations.ReviewDetails_Income)"></label>

See what I did there. It’s in my opinion way less readable, but the syntax highlighting is now correct.awkward syntax2
Wondering if anybody else has come across this glitch I went on the GoogleWebs and after a few minutes of research, I found this feedback ticket on the Microsoft Connect website.
They have taken it up with the Product Team, so I’ll be expecting a fix for this in the near future.


Regards,
Kyor