[英]How can i use model in Javascript?
Example : 示例:
<script type="text/javascript">
function abc() {
var t = document.getElementById("name").value;
@foreach(Player pl in Model){
if (t == pl.Name) {
alert("This name is already use!");
}
}
</script>
Error : t - cannot resolve symbol How can i use JavaScript variable in C# code? 错误:t - 无法解析符号如何在C#代码中使用JavaScript变量? Is it possible in this example? 在这个例子中是否可能?
You cannot use JS variables in C# code , but you can use C# variables in the code. 您不能在C#代码中使用JS变量,但可以在代码中使用C#变量。 In most cases C# is used to render HTML or JS. 在大多数情况下,C#用于呈现HTML或JS。
In your case it would be best if you render your C# serverside model as a JS array , which you can later on iterate. 在您的情况下,最好将C#服务器端模型渲染为JS数组,稍后可以迭代。
make an action that returns your list (assuming it's a list) as a JSON 制作一个动作, 将您的列表(假设它是一个列表)作为JSON返回
fetch the data with an AJAX get call to your action. 使用AJAX获取数据获取对您的操作的调用。
Cheers! 干杯!
You may do something like this. 你可能会做这样的事情。
<script type="text/javascript">
function abc(players) {
var t = document.getElementById("name").value;
for(p in players) {
if (t == p.Name) {
alert("This name is already use!");
}
}
abc(@Html.Raw(Json.Encode( Model )));
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.