使用Kendo UI Grid的Read
命令将ID传递给控制器的最佳方法是什么?必须从文本框中获取ID,并且必须通过单击按钮才能将数据加载到网格中。我更喜欢使用javascript和jQuery实现此目的。这是到目前为止我得到的代码:
控制器:
public ActionResult StrategyParameter_Read([DataSourceRequest]DataSourceRequest request, int? id)
{
IQueryable<StrategyParameter> strategyParam =
_db.StrategyParameter.Where(p => p.StrategySetId == id);
DataSourceResult result = strategyParam.ToDataSourceResult(request, sp => new {
Id = sp.Id,
StrategySetId = sp.StrategySetId,
ParamType = sp.ParamType,
Key = sp.Key,
Value = sp.Value,
MinimumValue = 0,
MaximumValue = 0,
IncrementalValue = 0
});
return Json(result);
}
视图:
<div class="form-group strategy-set-id">
<label class="col-sm-2 control-label" for="strategySetId">Strategy Set ID</label>
<div class="col-sm-4">
<input type="number" min="0" step="1" id="strategySetId" class="form-control" placeholder="Enter a Strategy Set ID">
<span class="help-block"></span>
</div>
<div class="col-sm-offset-2 col-sm-1">
<button type="submit" class="btn btn-primary" id="get-params">Get Parameters</button>
</div>
</div>
@(Html.Kendo().Grid<StrategyParameterViewModel>()
.Name("strategyParameters")
.Columns(columns =>
{
columns.Bound(c => c.Id).Width(170);
columns.ForeignKey(p => p.ParamType, (System.Collections.IEnumerable)ViewData["paramTypes"], "Key", "Value").Title("Param Type").Width(200);
columns.Bound(c => c.Key);
columns.Bound(c => c.Value);
columns.Bound(c => c.MinimumValue);
columns.Bound(c => c.MaximumValue);
columns.Bound(c => c.IncrementalValue);
})
//.ColumnMenu()
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable(pager =>
pager.Refresh(true)
)
.Navigatable()
.Resizable(resize => resize.Columns(true))
.Sortable(sortable =>
{
sortable.SortMode(GridSortMode.SingleColumn);
sortable.AllowUnsort(false);
})
.Filterable(filterable => filterable.Mode(GridFilterMode.Menu))
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(100)
.Model(model =>
{
model.Id(p => p.Id);
model.Field(p => p.Id).Editable(false);
})
.Read(read => read.Action("StrategyParameter_Read", "StrategySets"))
.Sort(sort => sort.Add("Id").Descending())
)
.Deferred()
)
最佳答案
如果在配置期间该值可用,则仅提供一个函数调用以返回Data属性中的数据。
.DataSource(dataSource => dataSource
.Ajax()
...
.Read(read => read.Action("StrategyParameter_Read", "StrategySets").Data("getMyID"))
...
)
JS功能
function getMyID()
return {
MyID:$("#tbID").val();
}
}
如果该值仅在初始网格配置之后才可用,则可以使用以下方法刷新网格:
grid.dataSource.read({ MyID:$("#tbID").val()});