2 回答
TA贡献1946条经验 获得超4个赞
控制器:
if (isExist != null)
{
TempData["Msg"] = "Your Attendance is Already Marked'"
}
看法:
<body>
@if (TempData["Msg"] != null)
{
<script type="text/javascript">
window.onload = function () {
alert(@TempData["Msg"]);
};
</script>
}
</body>
TA贡献1869条经验 获得超4个赞
为了显示我的消息,我这样做:
模型:
public class Alert
{
public const string TempDataKey = "TempDataAlerts";
public string AlertStyle { get; set; }
public string Message { get; set; }
public bool Dismissible { get; set; }
}
public class AlertStyle
{
public const string Success = "success";
public const string Information = "info";
public const string Warning = "warning";
public const string Danger = "danger";
}
我的基本控制器:
public class BaseController: Controller
{
public void Success(string message, bool dismissible = false)
{
AddAlert(AlertStyle.Success, message, dismissible);
}
public void Information(string message, bool dismissible = false)
{
AddAlert(AlertStyle.Information, message, dismissible);
}
public void Warning(string message, bool dismissible = false)
{
AddAlert(AlertStyle.Warning, message, dismissible);
}
public void Danger(string message, bool dismissible = false)
{
AddAlert(AlertStyle.Danger, message, dismissible);
}
private void AddAlert(string alertStyle, string message, bool dismissible)
{
var alerts = TempData.ContainsKey(Alert.TempDataKey)
? (List<Alert>)TempData[Alert.TempDataKey]
: new List<Alert>();
alerts.Add(new Alert
{
AlertStyle = alertStyle,
Message = message,
Dismissible = dismissible
});
TempData[Alert.TempDataKey] = alerts;
}
}
在我需要的任何控制器中就足够了:
public class PanelController : BaseController
{
public ActionResult Index()
{
Success($"Hello World!!!",true);
return View();
}
}
用于警报或消息的 PartialView
@{
var alerts = TempData.ContainsKey(Alert.TempDataKey)
? (List<Alert>)TempData[Alert.TempDataKey]
: new List<Alert>();
@*if (alerts.Any())
{
<hr />
}*@
foreach (var alert in alerts)
{
var dismissibleClass = alert.Dismissible ? "alert-dismissible" : null;
<div class="alert alert-@alert.AlertStyle @dismissibleClass">
@if (alert.Dismissible)
{
<button type="button" class="close pull-left" data-dismiss="alert" aria-hidden="true">×</button>
}
@Html.Raw(alert.Message)
</div>
}
}
最后:
<div class="mt-alerts">
@{ Html.RenderPartial("_Alerts"); }
</div>
添加回答
举报