页面加载时,我的模型会传入一台机器上的事件日志列表。我声明一个列表,然后用这些日志填充它。
private List<machine_log> _logObjList = new List<machine_log>();
这是通过以下方式填充的:
@helper FillLogMainList()
{
foreach (IQueryable<machine_log> logType in Model.MachineLogs)
{
foreach (var log in logType)
{
_logObjList.Add(log);
}
}
}
这些日志可以分为6种不同类型,如应用程序、系统、安全等。我想在 webgrid 中只显示一种类型,现在我已经做到了。问题是,当我已经加载了“应用程序日志”,但我想点击“安全日志”按钮来从我已经拥有的 _logObjList 列表中填充网格以显示安全类型的日志时,就会出现问题。
当我首次打开页面时,通过传递“application”作为类型字符串,使用此函数填充默认的“应用程序日志”:
@helper FillLogList(string type)
{
_logs.Clear();
foreach (var log in _logObjList)
{
if (log.log_type == type)
{
string entry = "";
if (log.entry_type == "Warning ")
{
entry = "http://trycatchgaming.com/temp/warning.png";
}
else
{
entry = "http://trycatchgaming.com/temp/error.png";
}
var newLine = new SingleLog
{
EntryType = entry,
Message = log.event_id + "#" + log.message,
Source = log.source,
EventId = log.event_id,
LogId = log.id,
EntryTime = log.entry_time
};
_logs.Add(newLine);
}
}
}
现在我有一个列表用于_logs,我用它来填充WebGrid,这是页面正常“body”部分的一部分,位于一个div内:
<div id="grid" style="padding: 0; overflow-y: scroll; height: 500px; width: 100%">
@obj = new WebGrid(source: _logs, defaultSort: "EntryTime", ajaxUpdateContainerId: "grid", rowsPerPage: 200000);
@obj.GetHtml(
tableStyle: "webgrid-table",
htmlAttributes: new { id = "MainTable" },
headerStyle: "webgrid-header",
//footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",
//mode: WebGridPagerModes.All,
columns: obj.Columns(
obj.Column(columnName: "EntryType", header: "Event Type", format:@<img src="@item.EntryType" />),
obj.Column(columnName: "EntryTime", header: "Event Time"),
obj.Column(columnName: "Source", header: "Source"),
obj.Column(columnName: "EventId", header: "Event Id"),
obj.Column(columnName: "LogId", header: "Log Id"),
obj.Column(header: "All", style: "labelcolumn", format: @<text><input class="check-box" tag="check-box" id="assignChkBx" name="Message" type="checkbox" value="@item.Message" /></text>)
)
)
</div>
因此,我的问题归结为,我知道如何清除并创建新的日志列表,但我不知道如何将其应用于网格。理想情况下,我可以清除日志列表,用安全日志填充它,然后刷新网格,现在网格将填充安全日志。反复操作。
然而,似乎没有内置的方法来处理数据。我能找到的所有示例都要求我返回控制器,获取更多数据,并通过某些部分视图重新填充网格。然而,我已经拥有所有数据,而且我更喜欢不必进行额外的查询和加载来获取我已经拥有的数据。