This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14302|Reply: 0

[ASP.NET] MVC implements a progress bar about time, using the jQuery ui's progressbar

[Copy link]
Posted on 10/5/2015 4:05:23 PM | | | |
In e-commerce websites, the progress bar is sometimes used to visually display whether a user is due and the user's current status.

Design such a model.

  1. public class User
  2.     {
  3.         public int Id { get; set; }
  4.         public string Name { get; set; }
  5.         public int CoopTime { get; set; }
  6.         public DateTime JoinTime { get; set; }
  7.     }
Copy code
The above is the cooperation duration attribute CoopTime and the jointime attribute JoinTime are two attributes that are closely related to progress.

In HomeController, an action method is used to display the interface, and an action method is used to receive GET requests from the view and return json data.
  1. public ActionResult Index()
  2.         {
  3.             return View();
  4.         }
  5.         public ActionResult GetStatus()
  6.         {
  7.             User user = new User()
  8.             {
  9.                 Id = 1,
  10.                 Name = "某某用户",
  11.                 CoopTime = 1,
  12.                 JoinTime = new DateTime(2014, 3, 20)
  13.             };
  14.             //判断合作是否已经到期
  15.             int result = DateTime.Compare(DateTime.Now, user.JoinTime.AddYears(user.CoopTime));
  16.             if (result <= 0) //当前时间比合作到期时间早,合作未到期
  17.             {
  18.                 //计算时间
  19.                 var pastDays = (DateTime.Now.Subtract(user.JoinTime)).TotalDays;
  20.                 var oneYearDays = (user.JoinTime.AddYears(user.CoopTime).Subtract(user.JoinTime)).TotalDays;
  21.                 var pastPercentage = (pastDays / oneYearDays) * 100;
  22.                 var dataSuccess = new { msg = true, p = pastPercentage };
  23.                 return Json(dataSuccess, JsonRequestBehavior.AllowGet);
  24.             }
  25.             else //当前时间比合作到期时间晚,合作已到期
  26.             {
  27.                 var dataFail = new { msg = false, p = 100 };
  28.                 return Json(dataFail, JsonRequestBehavior.AllowGet);
  29.             }
  30.         }
  31.     }
Copy code
Above,
● Use the static method of DateTime Compare to compare 2 times, one is the current time, the other is the join time + cooperation duration, if the result is less than or equal to 0, it means that there is no expiration.
● Use the static method of DateTime, Subtract, to subtract 2 times.

Home/Index.cshtml, when the page is loaded, send an asynchronous GET request to the server to display the returned data in the progressbar.
  1. <head>
  2.     <meta name="viewport" content="width=device-width" />
  3.     <title>Index</title>
  4.     <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
  5.     <scrip{过滤}t src="~/scrip{过滤}ts/jquery-1.8.2.min.js"></scrip{过滤}t>
  6.     <scrip{过滤}t src="~/scrip{过滤}ts/jquery-ui-1.8.24.min.js"></scrip{过滤}t>
  7.     <scrip{过滤}t type="text/javascrip{过滤}t">
  8.         $(function () {
  9.             
  10.             $.getJSON('@Url.Action("GetStatus","Home")', function(data) {
  11.                 if (data.msg == true) {
  12.                     var temp = parseInt(data.p);
  13.                     $('#p').progressbar({
  14.                         value: temp
  15.                     });
  16.                 } else {
  17.                     $('#message').text('已到期');
  18.                     $('#p').progressbar({
  19.                         value: parseInt(data.p)
  20.                     });
  21.                 }
  22.             });
  23.         });
  24.     </scrip{过滤}t>
  25. </head>
  26. <body>
  27.     <div id="p">      
  28.     </div>
  29.     <div>
  30.         <span id="message"></span>
  31.     </div>
  32. </body>
Copy code






Previous:Use the new version of Baidu Cloud to accelerate the protection of websites and block non-CDN IP addresses from accessing websites
Next:SoftEther VPN Client is a truly free VPN software
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com