通过控制器显示消息“出勤已标记”

我正在开发一个在线考勤门户,其中我在控制器中设置了一个条件,即用户不能每天两次标记考勤。他们每天只能记录一次出勤情况。因此,如果员工在同一日期第二次标记出勤,我想在视图页面“创建”上显示一条消息“出勤已标记”。我已经设置了一条警报消息,但我想在员工标记出勤的视图页面上显示一条消息。我已经搜索了很多,但找不到更好的。

这是我的控制器代码

 [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");

        }


www说
浏览 103回答 2
2回答

绝地无双

控制器:if (isExist != null){&nbsp; &nbsp;TempData["Msg"] = "Your Attendance is Already Marked'"}看法:<body>@if (TempData["Msg"] != null)&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<script type="text/javascript">&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.onload = function () {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(@TempData["Msg"]);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </script>&nbsp;&nbsp;}&nbsp;&nbsp;</body>

MMTTMM

为了显示我的消息,我这样做:模型:public class Alert&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public const string TempDataKey = "TempDataAlerts";&nbsp; &nbsp; &nbsp; &nbsp; public string AlertStyle { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string Message { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public bool Dismissible { get; set; }&nbsp; &nbsp; }public class AlertStyle&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public const string Success = "success";&nbsp; &nbsp; &nbsp; &nbsp; public const string Information = "info";&nbsp; &nbsp; &nbsp; &nbsp; public const string Warning = "warning";&nbsp; &nbsp; &nbsp; &nbsp; public const string Danger = "danger";&nbsp; &nbsp; }我的基本控制器:public class BaseController: Controller&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void Success(string message, bool dismissible = false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddAlert(AlertStyle.Success, message, dismissible);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Information(string message, bool dismissible = false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddAlert(AlertStyle.Information, message, dismissible);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Warning(string message, bool dismissible = false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddAlert(AlertStyle.Warning, message, dismissible);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Danger(string message, bool dismissible = false)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AddAlert(AlertStyle.Danger, message, dismissible);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void AddAlert(string alertStyle, string message, bool dismissible)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var alerts = TempData.ContainsKey(Alert.TempDataKey)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? (List<Alert>)TempData[Alert.TempDataKey]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : new List<Alert>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alerts.Add(new Alert&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AlertStyle = alertStyle,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Message = message,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dismissible = dismissible&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TempData[Alert.TempDataKey] = alerts;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }在我需要的任何控制器中就足够了:public class PanelController : BaseController&nbsp;{&nbsp; &nbsp; public ActionResult Index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;Success($"Hello World!!!",true);&nbsp; &nbsp; &nbsp; &nbsp;return View();&nbsp; &nbsp; }&nbsp;}用于警报或消息的 PartialView@{&nbsp; &nbsp; var alerts = TempData.ContainsKey(Alert.TempDataKey)&nbsp; &nbsp; &nbsp; &nbsp; ? (List<Alert>)TempData[Alert.TempDataKey]&nbsp; &nbsp; &nbsp; &nbsp; : new List<Alert>();&nbsp; &nbsp; @*if (alerts.Any())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; <hr />&nbsp; &nbsp; }*@&nbsp; &nbsp; foreach (var alert in alerts)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var dismissibleClass = alert.Dismissible ? "alert-dismissible" : null;&nbsp; &nbsp; &nbsp; &nbsp; <div class="alert alert-@alert.AlertStyle @dismissibleClass">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if (alert.Dismissible)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close pull-left" data-dismiss="alert" aria-hidden="true">×</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.Raw(alert.Message)&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; }}最后:&nbsp; &nbsp; <div class="mt-alerts">&nbsp; &nbsp; &nbsp; &nbsp; @{ Html.RenderPartial("_Alerts"); }&nbsp; &nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript