title: use jquery variable in @ block razor
body: <p>I'm strugling with a jquery script inside a cshtml page. For short my question is how to use a var inside a @ statement in a cshtml page?</p>
  
  <p>below an example of what I'm trying:</p>
  
  <pre><code>&lt;select id="DefaultText"&gt;
      &lt;option value="-1"&gt;-- select --&lt;/option&gt;
      @foreach( var d in Model.DefaultTexts )
      {
          &lt;option value="@d.Id" &gt;@d.Name&lt;/option&gt;
      }
  &lt;/select&gt;
  
  
  &lt;script type="text/javascript"&gt;
      $('#DefaultText').change(function () {
  
          var id = parseInt($('#DefaultText :selected').val());
          var text = @Model.DefaultTexts.First( t =&gt; t.Id == id );
  
          $('#CustomProductText').val(text);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>I can't reach the var id. It's out of scope. I've also tryed it with a for loop and a if statement. But in the if statement I get the same error: out of scope.</p>
  
  <p>The full story is this:
  On my page I've a dropdown list. The items to select are short names for default text parts. Based on the id or name, I want to show the default text part in a textbox.</p>
  
  <p>#CustomProductText is my textbox where the content should be placed (code not posted).</p>
  
  <p>I've also tryed it with @: and  statement but that did not work.</p>
  
  <p>What am I doing wrong or maybe its not even possible what I'm trying to do.</p>
  
  <p>As an alternative I've added a action to my controller to get the text form there. Below the code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $('#DefaultText').change(function () {
  
          var id = parseInt($('#DefaultText :selected').val());
          $.post("Categories/GetDefaultText", { Id: id }, function (data) {
              alert(data);
          });
  
  
          //$('#CustomProductText').val(text);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>controller code:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult GetDefaultText(int id)
      {
          using( var context = new MyContext() )
          {
              var text = context.DefaultText.First( d =&gt; d.Id == id ).Text;
              return this.Content( text );
          }
      }
  </code></pre>
  
  <p>This doesn't work. The action doesn't get hit in debug mode.</p>
  
  <p>regards,</p>
  
  <p>Daniel</p>