【问题标题】:Bootstrap Alerts in PartialViewPartialView 中的引导警报
【发布时间】:2016-02-19 13:56:23
【问题描述】:

我有 PartialView 并呈现指定的局部视图 - _GlobalPartialView。 我的 _GlobalPartialView.cshtml 的一部分:

<div class="alert alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>@result.Message</strong>
</div>

在我的部分视图上:

@Html.Partial(T4.Shared.Views._GlobalMessagesPartial)

在 PartialView 消息显示如下:

在这样的视图中:

在视图中我可以关闭警报框,但不能关闭 PartialView。为什么?如何解决?

【问题讨论】:

    标签: c# asp.net-mvc twitter-bootstrap view asp.net-mvc-partialview


    【解决方案1】:

    这不起作用的原因可能很少。

    为了确保您的 JQuery 可用于部分视图,您可以尝试在部分视图页面中添加 jquery 引用。希望你没有类似的问题http://forums.asp.net/t/1649526.aspx/1

    也参考这个Jquery or Javascript problem in my partial view

    【讨论】:

      【解决方案2】:

      所以这是相当老的,但我需要这样的东西,所以我决定发布我的解决方案。

      这是我的部分视图:

      @* Message Box *@
      @if (ViewBag.AlertMessage != null)
      {
      <div class="col-sm-12">
      
          @if (ViewBag.AlertDismissable == true)
          {
              <div class="alert alert-dismissible @ViewBag.AlertType" role="alert">
                  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                  </button>
                  @ViewBag.AlertMessage
              </div>
          }
          else
          {
              <div class="alert @ViewBag.AlertType" role="alert">@ViewBag.AlertMessage</div>
          }
      
          @*<div class="alert alert-success" role="alert">@ViewBag.Message</div>*@
      
      </div>
      

      }

      我的基类中有一些实用程序来设置 ViewBag:

          public void SetBSViewBagAlert(bsAlertType alerttype, string message)
          {
              SetBSViewBagAlert(alerttype, message, false);
          }
      
          public void SetBSViewBagAlert(bsAlertType alerttype, string message, bool dismissable)
          {
              // Translate from enum to constant to avoid typos... 
              switch (alerttype)
              {
                  case bsAlertType.success:
                      ViewBag.AlertType = ALERT_SUCCESS;
                      break;
                  case bsAlertType.info:
                      ViewBag.AlertType = ALERT_INFO;
                      break;
                  case bsAlertType.warning:
                      ViewBag.AlertType = ALERT_WARNING;
                      break;
                  case bsAlertType.danger:
                      ViewBag.AlertType = ALERT_DANGER;
                      break;
                  default:
                      ViewBag.AlertType = ALERT_INFO;
                      break;
              }
      
              ViewBag.AlertDismissable = dismissable;
              ViewBag.AlertMessage = message;
          }
      

      一旦我知道我有错误,我就会从控制器设置消息:

      SetBSViewBagAlert(bsAlertType.warning, ModelStateErrorsString(), true);
      

      这是视图中唯一需要的行:

      @Html.Partial("_AlertBoxPartial")
      

      我仍在对此进行调整。如果我做出更改,我会发布更新。

      【讨论】:

        猜你喜欢
        • 2017-01-22
        • 1970-01-01
        • 2022-08-06
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多