1

I get compile error at $(this).id)

<script>
            if ('@content.ToList().Count' > 0) 
            {
                $(".divContainer").text('@content.Where(p => p.ContainerID == `$(this).id`).Select(p => p.TextContents).First()');
            }
            else 
            {
                $(".divContainer").text("Please enter the text");

            }
</script>

How to add the id of 'this div' with class 'divcontainer' inside the razor code @content.Where(p => p.ContainerID == "ID".Select(p => p.TextContents).First()

question made simpler.

How to write the following code correctly

@string text = $(this).text();

Here razor variable 'text' is assigned with Jquery text value.

Jesna
  • 55
  • 2
  • 11
  • Do you have the backticks `\`$(this).id\`` like that in your code? – void Apr 19 '13 at 07:29
  • i wanted that particular code to be placed as a nonrazor code, so to emphasize that i used the quotes there.. – Jesna Apr 19 '13 at 08:15
  • 2
    If I understand correctly, I'm afraid what you are asking is not possible. You should keep in mind that razor produces HTML/JS (server-side) that is then rendered by the browser (client-side). The following code: `@string text = $(this).text();` is trying to assign a client-side value to a server-side variable. – Paolo Moretti Apr 19 '13 at 09:33

1 Answers1

0

use .attr() method of jquery to get the id

 if ('@content.ToList().Count' > 0) 
    {
        $(".divContainer").text('@content.Where(p => p.ContainerID == $(this).attr("id")).Select(p => p.TextContents).First()');
    }
    else 
    {
        $(".divContainer").text("Please enter the text");

    }
  • sorry this doesnt solve my issue, i need to insert a script code within a razor code... and the script code is $(this).attr("id").. i need the correct syntax – Jesna Apr 19 '13 at 07:10