为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

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 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>


查看完整回答
反对 回复 2023-07-29
  • 2 回答
  • 0 关注
  • 77 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信