试用了半天mvc,实在是不懂.不想写太多的apicontrol的继承.还是用老办法ashx,
前台查询使用
参数=={s:sdddd} && 参数<={s:xxxx} || 参数>={n:2016-05-06}
{s:}
前面是类型,后面是参数.
s:字符串,n:数值,b:布尔,d:日期
后台方法,支持,linq to ef,petopoco两种类型.解析代码.
public static string ConverUIWhere2Where(string Where,bool isef=true) { string oldWhere = Where; string[] whereArray = Where.Split(new string[] { "&&","||" }, StringSplitOptions.RemoveEmptyEntries); StringBuilder sb = new StringBuilder(); foreach (var whereItem in whereArray) { sb.Clear(); int wheretype = 0; var whereitemindex = whereItem.IndexOf("{n:"); if (whereitemindex < 0) { whereitemindex = whereItem.IndexOf("{d:"); wheretype = 3; } if (whereitemindex < 0) { whereitemindex = whereItem.IndexOf("{b:"); wheretype = 1; } if (whereitemindex < 0) { whereitemindex = whereItem.IndexOf("{s:"); wheretype = 2; } if (whereitemindex < 0) { sb.Append(whereItem); continue; } int beginindex = whereitemindex + 3; int endindex = whereItem.LastIndexOf("}"); var wherevalue = whereItem.Substring(beginindex, endindex - beginindex); sb.Append(whereItem.Substring(0, beginindex - 3)); switch (wheretype) { case 0://number { decimal wheredec = 0; decimal.TryParse(wherevalue, out wheredec); sb.Append(wheredec.ToString("#.##")); } break; case 1://boolean { bool whereb = wherevalue.Trim().ToUpper() == "TRUE"; sb.Append(whereb.ToString().ToLower()); } break; case 2://string { sb.Append("""); sb.Append(wherevalue.Trim()); sb.Append("""); } break; case 3: { //if (whereItem.IndexOf("<=") >= 0) //{ DateTime newtime = DateTime.Now; if (!DateTime.TryParse(wherevalue, out newtime)) newtime = DateTime.Now; sb.Append("Convert.ToDateTime(""); sb.Append(newtime.ToString("yyyy-MM-dd HH:mm:ss")); sb.Append("")"); //} } break; default: break; } sb.Append(whereItem.Substring(endindex + 1)); oldWhere = oldWhere.Replace(whereItem, sb.ToString()); } return oldWhere; }