Handle back-slash “\” in Razor View and JavaScript

We are passing following view bag value;

ViewBag.Alphabets = "a\\b\\c";

We are retrieving this value in JavaScript in Razor view;

console.log('@ViewBag.Alphabets');

JS Output
console.log('a\b\c');

Console Output
a_c

Where did the “\” goes? Since this is a part of escape characters so JS automatically removes “\\” and replaced it with “_”. “\b” is a metacharacter so it’s gone as well.

We can fix it by using Replace command on ViewBag.

console.log('@ViewBag.Alphabets.Replace("\\", "\\\\")');

JS Output
console.log('a\\b\\c;');

Console Output
a\b\c

References

https://www.tutorialspoint.com/escape-characters-in-javascript

FavoriteLoadingAdd to favorites

RECENT POSTS


Categories



Tags

ADO ai angular asian asp.net asp.net core azure ACA azure administration Azure Cloud Architect Azure Key Vault Azure Storage Blazor WebAssembly BLOB bootstrap Branch and Release flow c# c#; ef core css datatables design pattern docker excel framework Git HTML JavaScript jQuery json knockout lab LINQ linux power bi powershell smart home SQL Agent SQL server SSIS SSL SVG Icon typescript visual studio Web API window os wordpress


ARCHIVE


DISCLAIMER