问题描述
这是我的代码
MOdel:
public List < IDictionary > GetReports(string strEMPID,int strpresetID)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = new SqlCommand(SP_REPORT_PRESET,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ strEmpID,strEMPID);
cmd.Parameters.AddWithValue(@ strpresetid,strpresetID);
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
conn.Close();
返回ConvertToDictionary(ds.Tables [0]); ;
}
private List < IDictionary > ConvertToDictionary(DataTable dtObject)
{
var columns = dtObject.Columns.Cast < DataColumn > ();
var dictionaryList = dtObject.AsEnumerable()
.Select(dataRow => columns
.Select(column =>
new {Column = column.ColumnName ,Value = dataRow [column]})
.ToDictionary(data => data.Column,data => data.Value))。ToList()。ToArray();
返回dictionaryList.ToList < IDictionary > 跨度>();
}
控制器:
public ActionResult GetReports(string strEMPID,int strpresetID)
{
var result = fldHomeDAL.GetReports(strEMPID,strpresetID);
//列出< FLDHomeDetails > lstFLDHome = fldHome.GetFLDReportsList(dsReports);
返回PartialView(_ FLDExportReports,结果);
}
查看:
@using System。动态
@model列表< system.collections.idictionary>
@ {
布局= null;
}
@ {
var result = new List< dynamic>();
foreach(var emprow in型号)
{
var row =(IDictionary< string,>)new ExpandoObject();
Dictionary< string,> eachEmpRow =(Dictionary< string,>)emprow;
foreach(KeyValuePair< string,> everyEmpRow中的keyValuePair)
{
row.Add(keyValuePair);
}
result.Add(row);
}
var gridReport = new WebGrid(result,canSort:true,ajaxUpdateContainerId:gridReport,
rowsPerPage:5);
gridReport.Pager(WebGridPagerModes。继续返回); ;
}
@if(@Model!= null)
{
@ gridReport.GetHtml(tableStyle:webGrid,headerStyle:head,alternatingRowStyle:alt);
}
Here is my code
MOdel:
public List<IDictionary> GetReports(string strEMPID, int strpresetID) { SqlConnection conn = new SqlConnection(connStr); conn.Open(); SqlCommand cmd = new SqlCommand("SP_REPORT_PRESET", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@strEmpID", strEMPID); cmd.Parameters.AddWithValue("@strpresetid", strpresetID); DataSet ds = new DataSet(); SqlDataAdapter adp = new SqlDataAdapter(cmd); adp.Fill(ds); conn.Close(); return ConvertToDictionary(ds.Tables[0]); ; } private List<IDictionary> ConvertToDictionary(DataTable dtObject) { var columns = dtObject.Columns.Cast<DataColumn>(); var dictionaryList = dtObject.AsEnumerable() .Select(dataRow => columns .Select(column => new { Column = column.ColumnName, Value = dataRow[column] }) .ToDictionary(data => data.Column, data => data.Value)).ToList().ToArray(); return dictionaryList.ToList<IDictionary>(); }
Controller:
public ActionResult GetReports(string strEMPID, int strpresetID) { var result=fldHomeDAL.GetReports(strEMPID,strpresetID); //List<FLDHomeDetails> lstFLDHome = fldHome.GetFLDReportsList(dsReports); return PartialView("_FLDExportReports", result); }
view:
@using System.Dynamic
@model List<system.collections.idictionary>
@{
Layout = null;
}
@{
var result = new List<dynamic>();
foreach (var emprow in Model)
{
var row = (IDictionary<string,>)new ExpandoObject();
Dictionary<string,> eachEmpRow = (Dictionary<string,>)emprow;
foreach (KeyValuePair<string,> keyValuePair in eachEmpRow)
{
row.Add(keyValuePair);
}
result.Add(row);
}
var gridReport = new WebGrid(result, canSort: true, ajaxUpdateContainerId: "gridReport",
rowsPerPage: 5);
gridReport.Pager(WebGridPagerModes.NextPrevious); ;
}
@if (@Model != null)
{
@gridReport.GetHtml(tableStyle: "webGrid", headerStyle: "head", alternatingRowStyle: "alt");
}
这篇关于数据未绑定到webgrid。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!