Set model property in javascript variable

1

I have an Action Usuario that displays user details among other actions that can be performed. I also have a javascript code that needs to part of setting a model property in the javascript variable. Here is the code below:

@model Aprendendo.Asp.Net.Model.Usuario
//Códigos html
<script type="text/javascript">
    var idUsuario = @Model.Id;
    //Demais códigos
</script>

But that way it is not working. What to do in this situation?

    
asked by anonymous 23.07.2015 / 20:44

2 answers

2

Putting quotation marks around your property, as an example below

 @model Aprendendo.Asp.Net.Model.Usuario
 //Códigos html
 <script type="text/javascript">
  var idUsuario = '@Model.Id';
  //Demais códigos
 </script>
    
23.07.2015 / 21:19
4

Put the value of the model in a hidden field ( input type="hidden" ) and then assign the value of this hidden field in the JavaScript variable.

Razor Code:

<input type="hidden" id="codigo" value="@Model.Id" />

JavaScript code:

var id = $("#codigo").val();
    
24.07.2015 / 19:00