MVC 5 Ajax + bootstrap+ handle bar 例: 实现service 状态

Js Script

<script src="../../Scripts/handlebars-v1.3.0.js"></script>

 <script id="service-status-details-template" type="text/x-handlebars-template">

        {{#each values}}
        <div class="top-buffer-10">
            <div class="pull-left">{{ServiceName}}</div>
            <span style="margin-left: 15px;"> </span>
            <div class="pull-right">
                <span class="service-details-time">{{TimeDurationAgo}} AGO</span>
                {{#if IsUp}}
                <span class="glyphicon glyphicon-ok-sign" style="color: #5cb85c;"></span>
                {{else}}
                <span class="glyphicon glyphicon-exclamation-sign" style="color: #ff4f28;"></span>
                {{/if}}
            </div>
        </div>
        {{/each}}

    </script>
    <div style="z-index: 10;display: none;" id="service-status-details">
    </div>
    <a id="service-status" style="margin-left: 30%;" data-placement="bottom" href="#"></a>

    <script>
        var serviceStatusUrl = ‘@Url.Action("GetMonitoringServicesStatus")‘;

        $(document).ready(function () {
            $("#service-status").popover(
            {
                html: true,
                content: function () {
                    return $(‘#service-status-details‘).html();
                }
            });

            refreshServiceStatus();
            //window.setInterval(refreshServiceStatus, 5 * 60 * 1000 /*frequency set to 5 mins*/);
        });

        function refreshServiceStatus() { /*should be change to set interval*/
            $.ajax({
                type: ‘POST‘,
                url: serviceStatusUrl, /*this value is set in _monitoringServices.cshtml */
                success: function (data) {
                    applyToJsTemplate("service-status-details", "service-status-details-template", data);

                    if (!data.isOk) {
                        $("#service-status").attr("class", "btn glyphicon glyphicon-exclamation-sign");
                        $("#service-status").css("color", "#ff4f28");
                    } else {
                        $("#service-status").attr("class", "btn glyphicon glyphicon-ok-sign");
                        $("#service-status").css("color", "#5cb85c");
                    }

                },
                dataType: "json"
            });

        }

        function applyToJsTemplate(layoutId, templateId, data) {
            $("#" + layoutId).html(Handlebars.compile($("#" + templateId).html())(data));
        }
    </script>

Css

<style>
        .popover-content {
        background-color: #dadada;
    }
    .service-details-time {
        color: #adadad;
    }

    .service-details-name {
        color: #5d5d5d;
    }
    .popover {
        max-width: 100%;
    }

</style>

C#

  public JsonResult GetMonitoringServicesStatus()
        {
            if (DateTime.Now.Second % 2 == 0)
            {
                var serviceList = Builder<MonitoringServicesModel>.CreateListOfSize(5)
                    .TheFirst(1).With(m => m.ServiceName, "Active Directory").With(m => m.IsUp, false).With(m => m.TimeDurationAgo, "11 MINS")
                    .TheNext(1).With(m => m.ServiceName, "aZure.Microsoft.com").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "5 MINS")
                    .TheNext(1).With(m => m.ServiceName, "aZure Preview Portal").With(m => m.IsUp, false).With(m => m.TimeDurationAgo, "16 MINS")
                    .TheNext(1).With(m => m.ServiceName, "Management Portal").With(m => m.IsUp, false).With(m => m.TimeDurationAgo, "31 MINS")
                    .TheNext(1).With(m => m.ServiceName, "Network Infrastructure").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "1 MINS")
                    .Build();
                var allUp = serviceList.All(s => s.IsUp);
                return Json(new { isOk = allUp, values = serviceList }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var serviceList = Builder<MonitoringServicesModel>.CreateListOfSize(5)
                   .TheFirst(1).With(m => m.ServiceName, "Active Directory").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "1 MINS")
                   .TheNext(1).With(m => m.ServiceName, "aZure.Microsoft.com").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "26 MINS")
                   .TheNext(1).With(m => m.ServiceName, "aZure Preview Portal").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "16 MINS")
                   .TheNext(1).With(m => m.ServiceName, "Management Portal").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "5 SECONDS")
                   .TheNext(1).With(m => m.ServiceName, "Network Infrastructure").With(m => m.IsUp, true).With(m => m.TimeDurationAgo, "1 MINS")
                   .Build();
                var allUp = serviceList.All(s => s.IsUp);

                return Json(new { isOk = allUp, values = serviceList }, JsonRequestBehavior.AllowGet);
            }
        }

  public class MonitoringServicesModel
    {
        public string ServiceName { get; set; }

        public string TimeDurationAgo { get; set; }

        public bool IsUp { get; set; }
    }

时间: 2024-10-22 17:27:40

MVC 5 Ajax + bootstrap+ handle bar 例: 实现service 状态的相关文章

用最基本的EF+MVC+JQ+AJAX+bootstrap实现权限管理的简单实例 之登陆和操作权限

先来一堆关于上篇文章的废话(不喜者点此隐藏) 今天把博客加了个小功能,就是点标题可以隐藏或展示相关内容,做法很傻,就是引用了bootstrap的两个css类和全部的js文件,其实这样的小功能完全应该自己做的,主要还是因为前端差,还有就是懒.请大家不要太过在意命名和前端样式,我并没有进入公司工作,命名没有具体规范,都是随心所欲.前端实在太差,如果你觉得颜色样式太差,只能说明我的审美有问题,咱们主要看功能实现. 上篇文章发布后有一定的推荐量和浏量,对一个初学者来说,自认为还是挺不错的.最主要的是收到

Spring Ajax一个简单样例

配置不说了.要在前面helloworld的样例基础上弄. 相同在hello下新建ajax.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page isELIgnored ="false" %> <!DOCTYPE html PUBLIC "-//

ASP.NET MVC的Ajax.ActionLink 的HttpMethod=&quot;Get&quot; 一个重复请求的BUG

这段时间使用BootStrap+Asp.net Mvc5开发项目,Ajax.ActionLink遇到一个重复提交的BUG,代码如下: 1 @model IList<WFModel.WF_Temp> 2 @{ 3 Layout = null; 4 } 5 6 <!DOCTYPE html> 7 8 <html> 9 <head> 10 <meta name="viewport" content="width=device-w

Asp.Net MVC Unobtrusive Ajax

1.   Unobtrusive JavaScript介绍 说到Unobtrusive Ajax,就要谈谈UnobtrusiveJavaScript了,所谓Unobtrusive JavaScript即为非侵入式JavaScript(即将Js代码与html代码分离,方便阅读与维护),是目前在Web开发领域推行的一种思想. 2.   在ASP.NET MVC中的应用 2.1 概述 使用VS新建一个ASP.NET MVC项目就会在~/Scripts/目录下面看到很多以unobtrusive结尾的ja

MVC中Ajax post Json和Ajax Get

在MVC中用ajax的方式传送数据 先创建实体 using System.ComponentModel; namespace ViewerWeb.Models { public class UserInfo { [DisplayName("用户名:")] public string UserName { get; set; } [DisplayName("年 龄:")] public int Age { get; set; } [DisplayName("

spring mvc 和ajax异步交互完整实例(转自CSDN) 附下载地址

spring mvc 和ajax异步交互完整实例 spring MVC 异步交互demo: demo下载地址:http://download.csdn.net/download/quincylk/9521375 1.jsp页面: [java] view plain copy print? <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-

MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证

原文:MVC验证09-使用MVC的Ajax.BeginForm方法实现异步验证 MVC中,关于往后台提交的方法有: 1.Html.BeginForm():同步 2.Ajax.BeginForm():异步 3.js或jQuery提交后台 本文体验Ajax.BeginForm()方法.   View model using System; using System.ComponentModel.DataAnnotations;   namespace XHelent.Models { public

Bootstrap for MVC:Html.Bootstrap().TextBoxFor(model=&gt;model.Name)

在上篇博文中提到最近比较忙,也打过招呼Orchard系列文章更新速度可能会放缓,但还是会继续写下去,主要原因在最近想着开发一个新的东西(系统?组件?),等有一定成果时会跟大家分享一些相关的东西,今天介绍因为这个新项目而抽空写的一个类库,主要是在MVC中快速使用Bootstrap的. 为什么需要封装? Bootstrap 使用起来非常的简单便捷,那为什么还需要封装呢?Bootstrap 在使用过程中无非就是一些className和javascript,在之前的MVC开发中已经习惯了@Html.Te

MVC的Ajax异步请求

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @using (Ajax.BeginForm("GetTime","order",new AjaxOptions()           {               Confirm="你确认这么做吗?",               HttpMethod="post",               OnSuccess