PringlerRushing
PringlerRushing

Reputation: 41

ASP.NET Core MVC bootstrap modal - vary contents

I am trying to reproduce the following bootstrap modal example.

Each button has different data-x attributes that allow the modal to have different text for each button click.

The problem is that ASP.NET MVC Core does not allow me to use the @ character in the button contents. If i use a @ character as per the example, it wont compile but if i remove the @ it doesnt work.

(@ is reserved for the MVC Model data)

How do I make this work? Is there a way i can do like /@/ or something to allow me to use the @ character or is there an alternative?

Thanks

$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + recipient)
  modal.find('.modal-body input').val(recipient)
})
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="col-form-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 0

Views: 4957

Answers (2)

Ikram Shah
Ikram Shah

Reputation: 1246

As Mentioned by @Parvez you need to add @@ to allow the use of @

You need to do it for both data-whatever="@mdo" and Open modal for @mdo

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@@mdo">Open modal for @@mdo</button>

You can read more here

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-2.2

Upvotes: 1

Parvez
Parvez

Reputation: 187

If you really want to use @ character then use double @@ instead of single @. ex,

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@@mdo">Open modal for @mdo</button>

Upvotes: 1

Related Questions