如何从List <dynamic>构建一个html表,其中包含您在运行时之前不知道的col标头

时间:2017-01-17 21:34:47

标签: c# asp.net-mvc

如何运行以下方法并在局部视图中创建一个html表,并使用&#34; list&#34;中的列标题。我无法弄清楚的部分是如何为&#34; cols&#34;创建col标题?我来自:

 foreach (var stepItem in ChainSteps)
            {
                var x = pttDB.Directs_Details.Where(o => o.activity_detail_id == lineItem.id && o.chain_detail_id == stepItem.Chain_Detail_ID).FirstOrDefault();
                dynamicModel.Add(stepItem.Step, x.percentage);
            }

列表的类型为List<dynamic>

public ActionResult chainList(string chainCode, List<Activity_Details> oList)
    {

            var filterDirects = oList.Where(c => c.chainid == chainCode).ToList();
            var ChainSteps = eeDB.Chains_Detail.Where(c => c.chainID == chainCode).OrderBy(x => x.Order).ToList();
            var list = new List<dynamic>();
            //Loop the line item
            foreach (var lineItem in filterDirects)
            {

                dynamic expando = new ExpandoObject();
                var dynamicModel = expando as IDictionary<string, object>;
                var wbs = from w in eeDB.WBS_Structure
                          where w.id == lineItem.wbs_id
                          select new { wbsTask = w.Task_Subtask_Combine }.ToString();
                var activityCode = from w in eeDB.Activity_Basis
                                   where w.id == lineItem.activity_code_id
                                   select new { activityCode = w.Activity_Code }.ToString();
                var activityName = from w in eeDB.Activity_Basis
                                   where w.id == lineItem.activity_code_id
                                   select new { activityName = w.Description }.ToString();
                dynamicModel.Add("WBS", wbs);
                dynamicModel.Add("Activity Code", activityCode);
                dynamicModel.Add("Activity Name", activityName);
                dynamicModel.Add("PTT Descriptionn", lineItem.Description);
                dynamicModel.Add("Original Budget", lineItem.Qty * lineItem.Unit_Rate);
                foreach (var stepItem in ChainSteps)
                {
                    var x = pttDB.Directs_Details.Where(o => o.activity_detail_id == lineItem.id && o.chain_detail_id == stepItem.Chain_Detail_ID).FirstOrDefault();
                    dynamicModel.Add(stepItem.Step, x.percentage);
                }
                list.Add(dynamicModel);
            }
            return PartialView("_chainList", list);

    }

1 个答案:

答案 0 :(得分:1)

至少对于ExpandoObject ......

_chainlist.cshtml:

@model IDictionary<string,object>

<h1>A dynamic table...</h1>
<table>
<tr>
@foreach(var kvp in Model)
{
@:<th>@kvp.Key</th>
}
</tr>
<tr>
@foreach(var kvp in Model)
{
@:<td>@kvp.Value.ToString()</td>
}
</tr>
</table>