I have the following partial “InfoWindowContent” view.
@model CasWeb.Models.ViewModels.AssetLocation
<div>
<h2>@Model.Name</h2>
<table>
<tr>
<td>
Lat:
</td>
<td>@Model.Lat
</td>
</tr>
<tr>
<td>
Lng:
</td>
<td>@Model.Lng
</td>
</tr>
</table>
</div>
And I would like to use this as the content of a google maps info window. So I have been thinking something like this
//Get the content for info window for this asset from a a partial view
var infoWindowContent@(asset.Name) = "@Html.Action("InfoWindowContent", @asset)";
var infoWindow@(asset.Name) = new google.maps.InfoWindow({
content: infoWindowContent@(asset.Name),
maxWidth: 200
});
The problem I have is that java script complains of having a un terminated string literal, as the @html.Action method returns the partial view with all the line breaks in it and I end up with java script code like:
var infoWindowContentd4444444 = "<div>
<h2>d4444444</h2>
<table>
<tr>
<td>
Lat:
</td>
<td>43
</td>
</tr>
<tr>
<td>
Lng:
</td>
<td>43
</td>
</tr>
</table>
“;
I can get around this be removing all the line breaks from my partial view like:
@model CasWeb.Models.ViewModels.AssetLocation
<div><h2>@Model.Name</h2><table><tr><td>Lat:</td><td>@Model.Lat</td></tr><tr><td>Lng:</td><td>@Model.Lng</td></tr></table></div>
But this is a pain, and hard to maintain.
Any ideas how I can render the partial view as java script string?
Better you create a one more action and return PartialViewResult from the action. Made the Json request for this action and append the HTML response in the DIV. Please find more details here.