2012-09-12 3 views

Odpowiedz

30
<script type="text/javascript"> 
function SomeFunction() { 
    var array = @Html.Raw(Json.Encode(ViewBag.SomeEnumerable)); 
    for(var i = 0; i < array.length; i++) { 
     alert(array[i]); // should display "string1" then string2" 
    } 
} 
</script> 

będzie wyświetlany jako:

<script type="text/javascript"> 
function SomeFunction() { 
    var array = ["string1","string2"]; 
    for(var i = 0; i < array.length; i++) { 
     alert(array[i]); // should display "string1" then string2" 
    } 
} 
</script> 
+0

VS daje 'błędu składniowego;' kiedy zadeklarować var ​​tablicę = @ Html.Raw (Json.Encode (ViewBag.SomeEnumerable)); jednak o/p jest poprawne w html renderowane w przeglądarce –

+2

@IshaanPuniani, dlaczego ufasz VS Intellisense? To jest złe. Spójrz na renderowany znacznik, aby zobaczyć, że jest poprawny, mimo że VS mówi ci, że jest źle. –

Powiązane problemy