title: How do I pass data to modal view in ASP.NET MVC
body: <p>I am having problems passing data to modal view. Either modal view doesn't open at all or reads data from first item every time I open it.
  The biggest problem I have is that I don't know jQuery.
  I want a button to open a modal that reads a variable of my item in this case item.Code. Items are in a foreach loop.</p>
  <p>This is how i want my code to look.</p>
  <pre><code>&lt;div class=&quot;row&quot; style=&quot;padding: 0px 50px&quot;&gt;
  @foreach (var item in Model)
  {
      &lt;div style=&quot;width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block&quot;&gt;
          &lt;h4&gt;@item.Name&lt;/h4&gt;
  
          &lt;!-- Button to open modal view here for this specific item --&gt;
  
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>I tried it with and without a partial view.
  This is the code I currently have which just opens a modal window but doesn't show anything else.
  I know the problem is in id's, but I tried to change all id's to unique ones which caused even more problems.</p>
  <pre><code>    @foreach (var item in Model)
  {
      &lt;div style=&quot;width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block&quot;&gt;
          &lt;h4&gt;@item.Name&lt;/h4&gt;
      &lt;/div&gt;
      &lt;input class=&quot;btn btn-default&quot; type=&quot;button&quot; value=&quot;Details&quot; id=&quot;@item.id&quot; onclick=&quot;Details(this.id);&quot; /&gt;
  
  
      &lt;div class=&quot;modal fade&quot; id=&quot;myModal&quot; role=&quot;dialog&quot;&gt;
          &lt;div class=&quot;modal-dialog&quot; role=&quot;document&quot;&gt;
              &lt;div class=&quot;modal-content&quot;&gt;
                  &lt;div class=&quot;modal-header&quot;&gt;
                      &lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;Modal title&lt;/h5&gt;
                      &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
                          &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
                      &lt;/button&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;modal-body&quot;&gt;
                      @Html.Raw(item.Code);
                  &lt;/div&gt;
                  &lt;div class=&quot;modal-footer&quot;&gt;
                      &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <pre><code>&lt;script&gt;
      function Details(id) {
          $.get(&quot;@Url.Action(&quot;preview&quot;,&quot;Generator&quot;)/&quot;+id, function (data) { $('.modal-body').html(data); });
          $('#myModal').modal('show');
      }
  
      $('#myModal').on('hidden.bs.modal' , function (e) {
          $('.modal-body').html(&quot;&quot;);
      })
  &lt;/script&gt;
  </code></pre>
  <p>Thank you!</p>