Send data from file .cs or value of cshtml file to

2020-05-10 08:37发布

问题:

i have a question. Ex : I have student.cs , student.cshtml . I need get data of student.cs or student.cshtml to control.js in MVC. how to do that?

回答1:

I believe you're asking how to get data from either the View (student.cshtml) or Controller (student.cs (should be named StudentController.cs)) to a JavaScript file (control.js) to manipulate it in Javascript.

The way I've done this is through the View using a hidden div. Here is a generic example:

View:

<div style="display: none;" id="uniqueDivIdName" data-id="@JsonConvert.SerializeObject(@Model)" />
<script src="~/Scripts/control.js "></script>

Javascript:

var dataFromView = $("#uniqueDivIdName").data("id");