我相信我知道我遇到的问题;我只是不知道如何解决它。我的视图控制器有两个参数。首次加载时,它会获取一个参数并加载部分模型。一旦从页面上的网格中进行了选择,就会再次调用控制器方法,并使用两个参数加载模型并返回到视图。
这一切都很完美,但是当附加数据添加到模型时,它不会显示在屏幕上。当我在调试中运行它时,我在视图中看到了数据,它只是没有显示在屏幕上。
我的猜测是这是因为我需要重新加载页面,但我不完全确定如何做到这一点。另外,如果有人可以发表评论并让我知道我用来完成附加数据加载的工作流程是否正确,或者是否有更好的方法。
谢谢!
事件控制器
public class EventsController : Controller
{
//
// GET: /Events/
public ActionResult Events(string sName, int? iID)
{
ViewBag.AppName = sName;
EventReader ereader = new EventReader(sName);
Events _events = new Events();
_events.LoggedEvents = ereader.GetListOfEvents().ToDataTable();
//_events.EventDetails.Severity = "TEST";
if (iID != null)
{
_events.EventDetails = ereader.GetEvent((int)iID);
}
return View(_events);
}
public ActionResult Read([DataSourceRequest] DataSourceRequest request, string sLogName)
{
EventReader ereader = new EventReader(sLogName);
return this.Json(ereader.GetListOfEvents().ToDataTable().ToDataSourceResult(request));
}
}
事件.cshtml
@model Models.Events
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Events for @ViewBag.AppName</h2>
<!-- HEADER GRID -->
<div id="Events">
@(Html.Kendo().Grid(Model.LoggedEvents)
.Name("EventGrid")
.Events(events => events.Change("eventGridHandler"))
.Columns(columns =>
{
columns.Bound("EntryType");
columns.Bound("EventDate");
columns.Bound("Source");
columns.Bound("ID");
//foreach (System.Data.DataColumn column in Model.Columns)
//{
// columns.Bound(column.ColumnName);
//}
})
.Pageable()
.Sortable()
.Selectable(select => select
.Enabled(true)
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row)
)
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Field("EntryType", typeof(string));
model.Field("EventDate", typeof(DateTime));
model.Field("Source", typeof(string));
model.Field("ID", typeof (int));
model.Id("ID");
//foreach (System.Data.DataColumn column in Model.Columns)
//{
// model.Field(column.ColumnName, column.DataType);
//}
})
.Read(read => read.Action("Read", "Events")
.Data("eventGridBindingData")
)
))
</div>
<!-- DETAIL AREA -->
<div id="EventDetail">
<div id="Data">
@(Html.TextArea("EventData", Model.EventDetails.EventData))
</div>
<div id="Details">
<!-- Detail Table Goes Here -->
<table>
<tr>
<td>User</td>
<td>Location</td>
<td>Severity</td>
</tr>
<tr>
<td>@(Model.EventDetails.UserID)</td>
<td>@(Model.EventDetails.LocationID)</td>
<td>@(Model.EventDetails.Severity)</td>
</tr>
</table>
</div>
</div>
<!-- Page Scripts -->
<script type="text/javascript">
// Return additional data to the reader
function eventGridBindingData() {
return {
name: '@ViewBag.AppName'
};
}
// Handler for the EventGrid
function eventGridHandler(e) {
var data = this.dataItem(this.select());
var appName = '@ViewBag.AppName';
console.log("in event");
jQuery.ajax({
url: 'Events',
data: { 'sName' : appName, 'iID' : data.id },
type: "POST",
cache: false
});
}
</script>
Events.cs(模型)
public class Events
{
#region PROPERTIES
public DataTable LoggedEvents { get; set; }
public CEventMessage EventDetails { get; set; }
#endregion
#region CONSTRUCTORS
public Events()
{
LoggedEvents = new DataTable();
EventDetails = new CEventMessage();
}
#endregion
}