Home > Software engineering >  Pass model data to javascript function
Pass model data to javascript function

Time:02-11

I have this model @model WebApplication1.Models.Person which has an Id data. I want to pass Id to a javascript function via <a href="#" onclick=myFunction(idGoesHere)>.

I tried putting @Model.id or passing the value of @Model.id to another variable, but still it doesn't work. Any help would be appreciated.

CodePudding user response:

Your call to your JS function will look like this:

<a href="#" onclick="myFunction('@Model.id')"> </a>

This will send the current id in concern to your JS function.

CodePudding user response:

try to get raw value, and then use it

var userObj = @Html.Raw(Json.Encode(Model));
  • Related