ASP.NET Core MVC引导程序模式-内容不同

时间:2019-03-07 05:29:41

标签: jquery html asp.net-mvc asp.net-core bootstrap-modal

我正在尝试重现以下引导程序模式示例。

每个按钮具有不同的data-x属性,这些属性使模式每次单击按钮时可以具有不同的文本。

问题是ASP.NET MVC Core不允许我在按钮内容中使用@字符。如果我按照示例使用@字符,它将无法编译,但是如果我删除@则无法使用。

(@保留用于MVC模型数据)

我如何进行这项工作?有什么方法可以像/ @ /一样使用,或者可以让我使用@字符?

谢谢

$('#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>

2 个答案:

答案 0 :(得分:1)

如果您确实要使用@字符,请使用双@@而不是单@。例如,

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

答案 1 :(得分:1)

@Parvez提到,您需要添加@@以允许使用@

对于 data-whatever =“ @ mdo” @mdo的开放模式

,您都需要这样做
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@@mdo">Open modal for @@mdo</button>

您可以在此处阅读更多内容

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