亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過控制器顯示消息“出勤已標記”

通過控制器顯示消息“出勤已標記”

www說 2023-07-29 16:19:02
我正在開發一個在線考勤門戶,其中我在控制器中設置了一個條件,即用戶不能每天兩次標記考勤。他們每天只能記錄一次出勤情況。因此,如果員工在同一日期第二次標記出勤,我想在視圖頁面“創建”上顯示一條消息“出勤已標記”。我已經設置了一條警報消息,但我想在員工標記出勤的視圖頁面上顯示一條消息。我已經搜索了很多,但找不到更好的。這是我的控制器代碼 [Authorize]        public ActionResult Create()        {            Employee employee = JsonConvert.DeserializeObject<Employee>(User.Identity.Name);            return View(new Attendance() { Emp_Id = employee.Emp_Id });        }        [HttpPost]        public ActionResult Create(Attendance attendance)        {                          if (ModelState.IsValid)            {                try                {                    var attdate = attendance.Date;                    var nextdate = attdate.AddDays(1);                    var id = Convert.ToInt32(Session["UserID"]);                    var isExist = db.Attendance.FirstOrDefault(i => i.Emp_Id == id && i.Date == attdate && i.Date < nextdate);                                       if (isExist != null)                    {                   //Here i set the alert but i want to show message on view page.                        return Content("<script language='javascript' type='text/javascript'>alert('Your Attendance is Already Marked');</script>");                    }                    else                    {                        //var res = tempDate.Date;                        db.Attendance.Add(attendance);                        db.SaveChanges();                    }                }                catch (Exception ex)                {                    Console.WriteLine(ex.InnerException.Message);                }            }            return RedirectToAction("Index", "Attendance");        }
查看完整描述

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>


查看完整回答
反對 回復 2023-07-29
?
MMTTMM

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 [email protected] @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>


查看完整回答
反對 回復 2023-07-29
  • 2 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號