【问题标题】:Show a message through a controller "Attendance is already marked"通过控制器显示消息“已标记出勤”
【发布时间】:2020-11-13 05:50:35
【问题描述】:

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

这是我的控制器代码

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

【问题讨论】:

    标签: javascript jquery asp.net-mvc


    【解决方案1】:

    控制器:

    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>
    

    【讨论】:

      【解决方案2】:

      为了显示我的信息,我这样做:

      型号:

      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();
          }
       }
      

      用于警报或消息的部分视图

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

      【讨论】:

      • 谢谢这个回答也很有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多