2011-11-27 4 views
0

나는 스크립트의 텍스트는 컨트롤러 클래스에서 생성이다 runtime.The에 생성해야 자바 스크립트가 있습니다동적 자바 스크립트 3.0 + 면도기

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; 

내가 스크립트 태그를 사용하는 경우 :

<script type="text/javascript"> 

       function initialize() { 
       @Server.HtmlDecode(ViewData["MapString"].ToString()) 

       } 

</script> 

그것은 진정한 텍스트를 반환 나던 그것은이 같은 retruns :

contentString0 = ' < 테이블 폭 = " 100 % " style = " font-family : tahoma; 텍스트 정렬 : 오른쪽; 글꼴

** 업데이트 :이 사이트는 내가 "' <을"보여주고 싶은, 제대로 내 질문에 나타나지 않았다하지만 "보여 '<"

을하지만 반환해야합니다 contentString0 =' 을 '<'을 (를) '' <'(으)로 변환하십시오. 하지만 내가 사용할 때 : @ Server.HtmlDecode (ViewData [ "MapString"]. ToString()) 스크립트 태그에서 모든 것이 OK입니다.

+1

우선 무엇이 컨트롤러에서 자바 스크립트를 생성해야합니까? 멀리 볼 수 있듯이 Goole Maps API에 위도와 경도를 전달합니다. JSON 객체로 필요한 데이터 만 반환하는 Controller 액션을 만들면 어떨까요? – torm

+0

어떻게 할 수 있습니까? 예를 들려 줄 수 있습니까? 예 Google지도 dyanmicaly에 표시하고 싶습니다. – Shayan

답변

1

당신은 그것을 내가 컨트롤러에 코드를 생성보다 유연성이 될 것입니다 생각이 방법으로 수행 할 수 있습니다 :

컨트롤러 조치 : 뷰 추가에

public JsonResult GetCoords() 
    { 
     // your code here - im putting a generic result you may 
     // need to put some logic here to retrieve your location/locations 

     var result = new { lon = "51.0000", lat = "23.0000" }; 
     return Json(result, JsonRequestBehavior.AllowGet); 

    } 

:

<script type="text/javascript"> 
     $(document).ready(function() { 

      $.getJSON('/YourController/GetCoords', function (jsonData) { 

       var lon = jsonData.lon; 
       var lat = jsonData.lat; 

       yourGoogleMapFunction(lon, lat); 

      }); 
     }); 
    </script> 
+0

샘플의 getJSON이 작동하지 않습니다. JQuery 사이트에서 JQery1.7.js를 다운로드하지만 샘플이 작동하지 않았습니다. – Shayan

+0

그래서 난 내 컨트롤러 클래스에서 – torm

+0

을 당신을 도울 수 나에게 코드를 보여주십시오 : [HttpPost] 공공 JsonResult의 GetCoords는() {여기 // 코드 - 메신저 당신이 // 수있는 일반적인 결과를두고 일부를 둘 필요가 논리를 사용하여 위치/위치를 검색하십시오. var result = new {lon = "51.0000", lat = "23.0000"}; return Json (result, JsonRequestBehavior.AllowGet); 내 영문 파일에 } – Shayan