所以这是相当老了,但我需要这样的事情,所以我决定后我的解决方案。
这是我的局部视图:
@* 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")
我仍在调整各地与此有关。如果我进行更改,我会发布更新。