1

I want to create a div from every object in my model, and I need to give them unique IDs for the onclick-method. How do I do this? I tried to put myid inside <%= %> as I read somewhere, it does not work.

This is my code:

@foreach(var r in Model){
   var myid = r.ToString();

   <p class="expand-one" onclick="showhide('<%= myid  %>')" onmouseover="" style="cursor: pointer;">@r.ToSection.Name</p>
               <table id=@myid class="content-one">
                   <tr>
            ......................
             ..................
1
  • Do you need ids at all? Instead of passing <%= myid %> you could pass this.getElementsByTagName("table")[0] (the element rather than the string id) Commented Mar 13, 2015 at 13:09

2 Answers 2

3

You should be able to use Razor's @ in the same manner you use it in @r.ToSection.Name:

<p class="expand-one" onclick="showhide('@myid')"></p>
Sign up to request clarification or add additional context in comments.

Comments

1

Usually I use this, so you can try
<p class="expand-one" onclick="showhide('@(myid)')"></p>

1 Comment

You may want to elaborate your answer to be more holistic within a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.