Reputation: 14600
I have a string a characters (address) being passed from the viewmodel to the view. I am setting the address to a variable to be used by javascript/jquery. But if I have a string like
"Alamo Square Park, Steiner St & Hayes Street, San Francisco, CA 94117, USA"
when I set the variable in my razor view file like this
var viewModelFormattedAddress = @Html.Raw(Model.FormattedAddress);
I get an error!
If I just pass the value in quotes as a string to the variable it converts the special characters to non HTML characters like so
"Alamo Square Park, Steiner St & Hayes Street, San Francisco, CA 94117, USA"
if I do this
var viewModelFormattedAddress = "@Model.FormattedAddress";
I don't want this because then I have to convert it back.
Why is an error being thrown when I do a @Html.Raw() call?
UPdate -
here is what I'm trying
var viewModelFormattedAddress = @Html.Raw(Json.Encode(Model.FormattedAddress)).ToString();
this is the error
JavaScript critical error at line 297, column 41 in https://localhost:44368/Home/Events?date=7/19/2017&formattedaddress=Alamo+Square+Park,+Steiner+St+&+Hayes+Street,+San+Francisco,+CA+94117,+USA&latitude=37.7763694&longitude=-122.434819&maptype=establishment\n\nSCRIPT1002: Syntax error
and this is what it's showing in the dynamic page
var viewModelFormattedAddress = "Alamo Square Park, Steiner St \u0026 Hayes Street, San Francisco, CA 94117, USA";
Upvotes: 0
Views: 465
Reputation: 26016
I think you forgot the quotes... try this:
var viewModelFormattedAddress = "@Html.Raw(Model.FormattedAddress)";
Instead of
var viewModelFormattedAddress = @Html.Raw(Model.FormattedAddress);
Upvotes: 2