c#TempData警告消息不显示

时间:2018-12-03 16:54:51

标签: c# asp.net-mvc tempdata

我想了解为什么我的警告消息没有显示,所以我创建了通用类,控制器和cshtml。

  

控制器中是否缺少某些东西?

普通班

  SettingController controller = new SettingController();
  controller.ShowWarning("Warning on page");

控制器类

public ActionResult ShowWarning(string message)
        {

            TempData["Warning"] = message;
            // returning view counts as providing response
            return View();
        }

Alert.cshtml

<script type="text/javascript">
    $(document).ready(function() {
        @foreach (var key in Enum.GetNames(typeof(Messages)))
        {
            if (TempData.ContainsKey(key) || Session[key] != null)
            {
                var message = TempData[key] ?? Session[key];
                var type = "success";

                if (key == Messages.Warning.ToString())
                {
                    type = "notice";
                } else if (key == Messages.Danger.ToString())
                {
                    type = "error";
                }
                <text>
                new PNotify({
                    title: false,
                    text: '@Html.Raw(message.ToString().Replace("'", "\'").Replace("\r\n", "<br/>"))',
                    type: '@type',
                    styling: 'bootstrap3'                        
                });
                </text>
            }
        }
    });
</script>

0 个答案:

没有答案