《工作日记》--2019-01-17 动态绑定table
预览功能, 在你不知道要绑定数据那个表的情况下可以用以下操作
控制器层
public ActionResult DataDetailContentInfo(int ID = 0)
{
DataDetaViewModel viewmodel;
if(ID!=0)
{
…….
}else
{
viewmodel = new DataViewModel();
}
ViewData.Model = viewmodel;
if (!string.IsNullOrEmpty(viewmodel.Data_Script))
{
var scriptSite = viewmodel.Data_Script;//查询外部数据的语句
if (scriptSite != null)
{
//接收另一个数据的sql语句 用dataset存储
DataSet ds = commonService.QueryDataSet(scriptSite);
if (ds != null&&ds.Tables[0].Rows.Count>0)
{
ViewData[“data”] = ds;
}
}
}
ViewData[“Is_Outsite”] = viewmodel.Data_IsOutSide;
return View();
}
视图层
<div class=”modal-body”>
<div class=”tableList”>
@if (int.Parse(ViewData[“Is_Outsite”].ToString()) == 1)
{
<table class=”ui-jqgrid-btable ui-common-table table table-bordered” style=” background:#ffffff!important; margin-bottom:30px;”>
<thead>
@*循环标题*@
<tr class=”jqgfirstrow”>
<th><input type=”checkbox” class=”checkall” /></th>
@foreach (DataColumn item in (ViewData[“data”] as DataSet).Tables[0].Columns)
{
<th>@item</th>
}
</tr>
</thead>
<tbody>
@*循环数据的多个行*@
@foreach (DataRow DRows in ((DataSet)ViewData[“data”]).Tables[0].Rows)
{
<tr class=”jqgfirstrow”>
<td><input type=”checkbox” class=”checkchild” value=”” /></td>
@*循环数据的多个列*@
@for (int i = 0; i < ((DataSet)ViewData[“data”]).Tables[0].Columns.Count; i++)
{
<td style=”height:20px;line-height:20px;”>@DRows.ItemArray[i]</td>
}
</tr>
}
</tbody>
</table>
}
</div>
</div>