所以這是相當老了,但我需要這樣的事情,所以我決定後我的解決方案。
這是我的局部視圖:
@* 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">×</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")
我仍在調整各地與此有關。如果我進行更改,我會發布更新。