J'ai un javascript qui doit générer dans le texte runtime.The du script est en classe de générer contrôleur:javascript dynamique dans ASP.Net MVC 3.0 + Razor
private string mapString
{
get
{
Locations loc = new Locations();
string appPath = Request.ApplicationPath;
loc.ReadXml(Path.Combine(Request.MapPath(appPath) + "\\App_Data", "Locations.xml"));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < loc.Specfications.Count; i++)
{
sb.Append("var myLatLng" + i.ToString() + "= new google.maps.LatLng(" + loc.Specfications[i].Y.ToString() + "," +
loc.Specfications[i].X.ToString() + ");");
sb.Append(" var beachMarker" + i.ToString() + " = new google.maps.Marker({position: myLatLng" + i.ToString() + ",map: map,icon: image,title:'" + loc.Specfications[i].Title + "'});");
....
...
...
ViewData["MapString"] = mapString;
Quand je l'utilise dans la balise de script:
<script type="text/javascript">
function initialize() {
@Server.HtmlDecode(ViewData["MapString"].ToString())
}
</script>
il ne marche pas retourner un vrai texte et retruns quelque chose comme ceci:
contentString0 = ' < largeur de table = " 100% " style = " famille de polices: tahoma; text-align: droit; police
** Mise à jour: Le site n'a pas montré ma question correctement, je veux montrer "' <" mais il montre " » <"
mais il doit retourner: contentString0 = 'vous voir qu'il convertit "'<" en "' <". Mais quand j'utilise: @ Server.HtmlDecode (ViewData ["MapString"]. ToString()) à partir de balise de script, tout est OK.
Tout d'abord, pourquoi auriez-vous besoin de générer votre javascript dans votre contrôleur? Autant que je peux voir que vous passez la longitude et la latitude à Goole Maps API. Pourquoi ne pas créer une action Contrôleur qui ne retournerait que les données dont vous avez besoin en tant qu'objet JSON? – torm
Comment puis-je le faire? Pouvez-vous donner un exemple? Oui, je veux marquer sur google map dyanmicaly. – Shayan