記錄一些常用的知識點吧
1、js前端頁面獲取後臺返回的list集合
var topupList = ViewBag.TopupList;
function TopupPage(pageIndex){
var topupJson = @Html.Raw(Json.Encode(topupList));
var list = eval(topupJson);
}
2、時間戳格式化時間格式
function formatDateBoxFull(value) { if (value == null || value == '') { return ''; } var dt = parseDate(value); //console.log(dt.getFullYear()); return dt.getFullYear() == 1 ? "— —" : dt.format("yyyy-MM-dd hh:mm:ss"); } var parseDate = function (timeSpan) { var timeSpan = timeSpan.replace('Date', '').replace('(', '').replace(')', '').replace(/\//g, ''); var d = new Date(parseInt(timeSpan)); return d; }; //為Date型別拓展一個format方法,用於格式化日期 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, //month "d+": this.getDate(), //day "h+": this.getHours(), //hour "m+": this.getMinutes(), //minute "s+": this.getSeconds(), //second "q+": Math.floor((this.getMonth() + 3) / 3), //quarter "S": this.getMilliseconds() //millisecond }; if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); return format; }; 運用: $("#RefundTime").val(formatDateBoxFull(list[pageIndex].RefundTime));
3、a標籤點選變紅,其他的恢復原狀
@for (int i = 0; i < topupList.Count; i++){
var id = "TopupPage" + i;
<a id="@id" class="TopupPage" style="padding-left:5px;" onclick="TopupPage(@i)">@(i + 1)</a>
}
var alist = document.getElementsByClassName("TopupPage");
$(".TopupPage").css({color:"#000"});
$("#TopupPage"+pageIndex).css({color:"#ff0000"}); //令當前標籤高亮
4、js動態時間
$(function () {
GetDate();
setInterval("GetDate()", 1000);
});
function GetDate() { var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); var timeStr = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day) + " " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second); //console.log(timeStr); $("#RealTime").html(timeStr); }
5、鍵值對使用
Dictionary<int, List
if (!dic.ContainsKey(year)) //判斷鍵值對中是否存在某個鍵
dic.Add(year, dataList);
dataList = new List
ViewBag.DicList = dic.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value); //將鍵值對按key值降序排序
6、檔案上傳功能
[HttpPost]
/// <summary>
/// 上傳檔案
/// </summary>
/// <returns></returns>
public JsonResult ExportFile()
{
UploadFile _uploadFile = new UploadFile();
try
{
var file = Request.Files[0]; //獲取選中檔案
string fileNameEx = Path.GetExtension(file.FileName);//獲取副檔名
string fileNameTrue = Path.GetFileNameWithoutExtension(file.FileName);// 獲取檔名不含副檔名
string imgType = Configs.GetValue("ImgType").ToUpper();
if (file == null || string.IsNullOrEmpty(file.FileName) || file.ContentLength == 0)
{
_uploadFile.code = -1;
_uploadFile.data = new { src = "" };
_uploadFile.msg = "上傳出錯!未檢測到檔案";
return Json(_uploadFile);
}
if (!imgType.Contains(fileNameEx.ToUpper().Replace(".", "")))
{
_uploadFile.code = -1;
_uploadFile.data = new { src = "" };
_uploadFile.msg = "上傳出錯!圖片格式只支援"+ imgType;
return Json(_uploadFile);
}
string filePathName = string.Empty;
//定義本地路徑位置
string localPath = Server.MapPath("~/Upload/ArticleUploadImg/");
string tmpName = Server.MapPath("~/Upload/ArticleUploadImg/");
var tmp = file.FileName;
var tmpIndex = 0;
//判斷是否存在相同檔名的檔案 相同累加1繼續判斷
while (System.IO.File.Exists(tmpName + tmp))
{
tmp = fileNameTrue + "_" + ++tmpIndex + fileNameEx;
}
//不帶路徑的最終檔名
filePathName = tmp;
if (!Directory.Exists(localPath))
Directory.CreateDirectory(localPath);
file.SaveAs(Path.Combine(tmpName, filePathName)); //儲存圖片(資料夾)
_uploadFile.code = 0;
_uploadFile.data = new { src = Path.Combine("/Upload/ArticleUploadImg/", filePathName), title = Path.GetFileNameWithoutExtension(filePathName) };
_uploadFile.msg = "上傳成功";
return Json(_uploadFile);
}
catch (Exception)
{
return Json(_uploadFile, JsonRequestBehavior.AllowGet);
}
}
7、List 合併操作
List
List
List
List