I am creating a web app in which, I want to pass model data into a Javascript function when user click a button,
Below is my code
<input type="button" id="btnNextAction" class="btn btn-primary" value="JSFunction" onclick="callJS('@Model.NextActionParam');" />
Javascript Function
function callJS(param) {
}
but the value of param is appears as projectName.Models.modelName.NextActionParam
model.NextActionParam is my model which contains some properties like name, id.etc
how can I get the model property with value in Javascript?
You need to convert Model.NextActionParam
to JSON string with Json.Encode()
method. Better way is put JSON inside <script>
section and set some variable, then use it anywhere.
<script>
var nextActionParam = @Html.Raw(Json.Encode(Model.NextActionParam));
</script>
<input type="button" id="btnNextAction" class="btn btn-primary" value="JSFunction"
onclick="callJS(nextActionParam)" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.