ASP.NET MVC 增强Convert用法+【分页2】

【分页2】

public dynamic PageQuery()

{

int pageIndex = Request.Params["page"] == null ? 1 : int.Parse(Request.Params["page"]);//Request["page"] 需要显示第几页 EasyUI datagrid自传

int pageSize = Request.Params["rows"] == null ? 10 : int.Parse(Request.Params["rows"]);//每页大小 EasyUI datagrid自传

try {

BackUserInfoPageCondition condition = new BackUserInfoPageCondition

{

pageIndex=pageIndex,

pageSize=pageSize,

UserCode = Request.Params["search_UserCode"],

UserName = Request.Params["search_UserName"],

IDNO = Request.Params["search_IDNO"],

Phone = Request.Params["search_Phone"],

DepartId = Request.Params["search_Depart"].Trim().ConvertTo<int>(),//默认-1请选择

DutyId = Request.Params["search_Duty"].Trim().ConvertTo<int>() ,

StatusFlag = Request.Params["search_StatusFlag"].Trim().ConvertTo<short?>(),

DimissionFlag = Request.Params["search_DimissionFlag"].Trim().ConvertTo<short?>(),

DisableFlag = Request.Params["search_DisableFlag"].Trim().ConvertTo<short?>(),

LockFlag = Request.Params["search_LockFlag"].Trim().ConvertTo<short?>()

};

var data = _IBackUserInfoService.PageQuery(condition);//根据条件query查询

var o = new { total = condition.total, rows = data };//EasyUI datagrid需要

return Json(o);

}

catch (Exception e)

{

//写日志

return Content("ERROR");

}

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

using System.Web;

namespace Common

{

public static class Utils

{

/// <summary>

/// MD5加密

/// https://social.msdn.microsoft.com/Forums/zh-CN/590bd6a8-57d7-4041-81da-80fe8b832b77/md5

/// http://blog.163.com/m13864039250_1/blog/static/21386524820150231533602/

/// </summary>

public static string GetMD5Hash(string input){

//MD5 md5Hash = MD5.Create();

MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

byte[] data=md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

StringBuilder sBuilder = new StringBuilder();

for (int i = 0; i < data.Length; i++)

{

sBuilder.Append(data[i].ToString("x2"));//字符串格式控制符 x 为十六进制 2每次两位数

}//0xA、0x1A,这样看起来不整齐,为了好看,可以指定"X2",这样显示出来就是:0x0A、0x1A。

return sBuilder.ToString();

}

/// <summary>

/// 任意类型之间的转换

/// 来源:http://www.cnblogs.com/artech/archive/2011/03/17/NullableType.html

/// 用法:

/// Utils.ConvertTo<short?>(Request.Params["LockFlag"])

/// Request.Params["LockFlag"].ConvertTo<short?>()

/// int intValue1 = "123".ConvertTo<int>();

/// int? intValue2 = "123".ConvertTo<int?>();

/// DateTime dateTimeValue1 = "1981-08-24".ConvertTo<DateTime>();

/// DateTime? dateTimeValue2 = "1981-08-24".ConvertTo<DateTime?>();

/// </summary>

public static T ConvertTo<T>(this IConvertible convertibleValue)

{

if (null == convertibleValue)

{

return default(T);

}

if (!typeof(T).IsGenericType)

{

return (T)Convert.ChangeType(convertibleValue, typeof(T));

}

else

{

Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition();

if (genericTypeDefinition == typeof(Nullable<>))

{

return (T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T)));

}

}

throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, typeof(T).FullName));

}

}

}