在我看来,我有一个网格(来自KendoUI),我想根据复选框的状态更新。我的想法是,只要单击它,我就想将复选框的值(bool)发送到我的控制器,然后使用复选框中的bool值作为参数调用提供我的数据的服务。从那里,我想刷新已经使用服务调用中的新数据填充网格的列表。
我尝试了一些ajax调用,但我似乎无法解决这个问题。
这是我的索引视图:
<div class="row-fluid">
<div class="span12">
<div class="k-block">
<div class="k-header">Unit List</div>
@Html.CheckBox("mycheckbox")
@(Html.Kendo().Grid(Model.UnitTypes)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Id).Groupable(false);
columns.Bound(p => p.Name);
columns.Command(command => { command.Custom("Edit Unit"); }).Width(160);
})
.Groupable()
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
)
}
</div>
</div>
这是我的控制器:
public ActionResult Index()
{
var client = new UnitServiceClient();
var listOfUnitsFromService = client.GetListOfUnits(true);
var model = new UnitModel
{
UnitTypes = listOfUnitsFromService.ToList()
};
return View(model);
}
希望有人可以提供帮助,因为当谈到ajax调用时我完全是空白的。)
答案 0 :(得分:2)
您要做的是创建一个接受布尔值的控制器操作,并返回包含网格所有HTML的PartialView。
然后,您的Ajax将调用该控制器操作,传入布尔值并从服务器接收网格HTML。然后它将使用新的HTML更新包含网格的div。
<强>控制器强>
[HttpPost]
public PartialViewResult Blah(bool someFlag)
{
// Standard controller code. Load a model. Return a PartialView.
}
<强>的Ajax 强>
$.ajax({
type: "post",
dataType: "html",
url: '/Home/Blah',
data: 'someFlag=' + $('#mycheckbox').val(),
success: function (response) {
$('#k-block').html(response);
},
});
答案 1 :(得分:0)
好的,所以我找到了解决方案,至少目前是这样。 结束将复选框和我的FormsCollection一起传递给控制器,然后根据其“开/关”值进行处理。
我的表单只是使用以下标记行提交:
<input type="checkbox" name="refresh" class="k-checkbox" id="refresh" onclick="document.forms.refreshForm.submit();"/>
然后,我基于模型中的“已禁用”属性设置了我的行样式,如下所示:
@(Html.Kendo().Grid(Model.UnitTypes)
.Name("Grid")
.RowAction(row =>
{
if (row.DataItem.Disabled)
{
row.HtmlAttributes["style"] = "background-color: LightGray";
}
})
.Columns(columns =>
{
columns.Bound(p => p.Id).Groupable(false);
columns.Bound(p => p.Name);
columns.Bound(p => p.Disabled);
columns.Command(command => { command.Custom("Edit Unit"); }).Width(160);
})
.Groupable()
.Pageable()
.Sortable()
.Scrollable()
.Filterable())