开发者

JavaScript literal insert vaule? (Too many characters in character literal)

开发者 https://www.devze.com 2023-01-28 10:02 出处:网络
Ok so in my mvc View my \"Model\" is a list of objects and in this case i want to get a specific Id of one of the objects.

Ok so in my mvc View my "Model" is a list of objects and in this case i want to get a specific Id of one of the objects.

开发者_如何学运维
var counter = 0; //for example

var id = '<%=Model[' + counter + '].Id %>';

This is what i get :

Compiler Error Message: CS1012: Too many characters in character literal

Thanks!


Yes, the counter variable can't be used as the index for Model[], because it only exists on the client (assuming this snippet was inside of a script html element). When you are creating a view, it might be useful to think of it as a long 'string' that you are constructing (the html returned to the browser). In this case 'counter' is just some characters in your script block that will eventually be interpreted by the browser, so it's not a variable that can be used while building the view. The <%= %> block just returns a string to append (that's what the = sign does).

Perhaps you intended to have the counter variable in it's own C# code block like so:

<% var counter = 0; %>
<script language=javascript>
  var id = '<%=Model[counter].Id %>';
</script>


That doesn't really make any sense. You're trying to put together an ASP expression with client-side Javascript in the middle of it. It's nonsensical because the ASP stuff all runs on your server, while the Javascript runs in the client.

Without knowing the rest of what you want to do, it's hard to say how you should proceed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号