Literary insert vaule? (Too many characters in a literal character)

Ok, so in my mvc. "My model" is a list of objects, in which case I want to get the specific identifier of one of the objects.

var counter = 0; //for example var id = '<%=Model[' + counter + '].Id %>'; 

Here is what I get:

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

Thanks!

+4
source share
2 answers

Yes, the counter variable cannot be used as an index for Model [], since it exists only on the client (if this fragment was inside the html script element). When you create a view, it may be useful to think of it as a long string that you create (html returns to the browser). In this case, the β€œcounter” is just a few characters in your script block that will ultimately be interpreted by the browser, so this is not a variable that can be used to build the view. The <% =%> block simply returns the string to add (which makes the = sign).

Perhaps you intended to have a counter variable in your own C # code block as follows:

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

It makes no sense. You are trying to assemble an ASP expression with client-side Javascript in the middle of it. This is pointless because all ASP content runs on your server, and Javascript runs on the client.

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

0
source

All Articles