using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
namespace System.Web.Mvc
{
public class TableColumn
{
public string ColumnName;
public string ColumnField;
public Func<DataRow,string> GenerateHTML;
public TableColumn(string _columnName, string _columnField)
{
ColumnField = _columnField;
ColumnName = _columnName;
//new TableColumn(_columnName, _columnField, null);
}
public TableColumn(string _columnName, string _columnField, Func<DataRow, string> _generateHtml)
{
ColumnField = _columnField;
ColumnName = _columnName;
GenerateHTML = _generateHtml;
}
}
public static class HtmlExtended
{
public static string Table(this System.Web.Mvc.HtmlHelper T1,List<TableColumn> list1,DataTable dt)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table class='tdare' cellspacing='0' width='100%'><tr class='tatr1'>");
foreach (TableColumn tc in list1)
{
sb.Append(string.Format("<td>{0}</td>", tc.ColumnName));
}
sb.Append("</tr>");
foreach (DataRow dr in dt.Rows)
{
sb.Append("<tr class='tatr2'>");
foreach (TableColumn tc in list1)
{
string columnValue = "";
if (tc.GenerateHTML == null)
{
columnValue = dr[tc.ColumnField].ToString();
}
else
{
columnValue = tc.GenerateHTML(dr);
}
sb.Append(string.Format("<td>{0}</td>",columnValue));
}
sb.Append("</tr>");
}
sb.Append("</table>");
return sb.ToString();
}
}
}
在 View 中调用:
<%
List<TableColumn> list1=new List<TableColumn>();
list1.Add(new TableColumn("项目","SysDesc"));
list1.Add(new TableColumn("代理方式","ProxyTypeDesc"));
list1.Add(new TableColumn("代理城市","CityName"));
list1.Add(new TableColumn("行业","ProfessionName"));
list1.Add(new TableColumn("支付状态","PayStateDesc"));
list1.Add(new TableColumn("审核状态","AuditStateDesc"));
list1.Add(new TableColumn("代理费用","Amount"));
list1.Add(new TableColumn("操作", "", delegate(DataRow dr)
{
if (dr["PayState"].ToString() == "0")
{
string parm = string.Format("/MerchantsOrders/DoPay?RealName={0}&Amount={1}&OrderID={2}&ProductName={3}&Quantity={4}",
System.Web.HttpUtility.UrlEncode(dr["RealName"].ToString()),
dr["Amount"].ToString(),
dr["ID"].ToString(),
System.Web.HttpUtility.UrlEncode((dr["productName"].ToString()))
, 1);
return "<a href='" + parm + "' target='_blank'>支付</a>";
}
return "";
}));
string html = Html.Table(list1, dt);
Response.Write(html);
%>