gpt4 book ai didi

c# - 如何在 mvc kendo 网格的 ajax 绑定(bind)中使用客户端模板表达式?

转载 作者:太空狗 更新时间:2023-10-29 21:39:51 24 4
gpt4 key购买 nike

我有一个两层的分层网格,我正在从服务器端绑定(bind)转移到使用 ajax。两层数据的 ajax 读取均正常工作,但我无法使用 ClientTemplate 根据条件逻辑呈现我的列。

以下是服务器端绑定(bind)版本。我知道我必须使用 ClientTemplate 和表达式 #=# 来达到相同的效果,但我遇到了两个问题:

  1. 如何为每一行递增变量“i”,以便我可以使用 CheckBoxFor 和类似的 html 辅助方法?
  2. 如何将 @ 转换为使用 ClientTemplate 表达式。请注意,条件逻辑使用 View 模型的属性以及绑定(bind)元素 (MyViewModel) 的属性,条件逻辑使用来自模型的属性的混合

将其转换为表达式会很有帮助。

var i = -1;

@(Html.Kendo().Grid<MyViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.Selected).Title("")
.Template(
@<text>
@{i++;}
@if (Model.Permissions.HasInsertAccess && item.Status == Status.Created)
{
<input type="hidden" name="MyViewModels.Index" value="@i" />
@Html.CheckBoxFor(m => m.MyViewModels[i].Selected)
}
</text>);

columns.Bound(c => c.Id)
.Template(@<text>@Html.HiddenFor(m => m.MyViewModels[i].Id)@item.Id</text>)

最佳答案

请尝试使用以下代码片段。

查看

@model MvcApplication1.Models.TestModels

<script type="text/javascript">
var rowNumber = 0;

function resetRowNumber(e) {
rowNumber = 0;
}

function renderNumber(data) {
return ++rowNumber;
}

function renderRecordNumber(data) {
var page = parseInt($("#Grid").data("kendoGrid").dataSource.page()) - 1;
var pagesize = $("#Grid").data("kendoGrid").dataSource.pageSize();
return parseInt(rowNumber + (parseInt(page) * parseInt(pagesize)));
}

</script>
@(Html.Kendo().Grid<MvcApplication1.Models.TestModels>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.ID);
columns.Bound(p => p.Name);

columns.Template(t => { }).Title("Row No").ClientTemplate("# if ( '" + @Model.Permissions.HasValue.ToString().ToLower() + "' == 'true') { #" +
"<input type='text' name='MyViewModels.Index' value='#= renderNumber(data) #' /> " +
"# } #");

})
.Pageable(x => x.PageSizes(new int[] { 10, 20, 30, 50 }).Refresh(true))
.Sortable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Grid_Read", "Home"))

)
.Events(ev => ev.DataBound("resetRowNumber"))
)

Controller

public ActionResult Index()
{
TestModels model = new TestModels();
model.Permissions = true; //Please comment this line and check
return View(model);
}

public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request)
{
List<TestModels> models = new List<TestModels>();

for (int i = 0; i < 50; i++)
{
TestModels t1 = new TestModels();
t1.ID = i;
t1.Name = "Name" + i;
models.Add(t1);

}

return Json(models.ToDataSourceResult(request));
}

模型

public class TestModels
{
[Display(Name = "ID")]
public int ID { get; set; }

[Display(Name = "Name")]
public string Name { get; set; }

public bool? Permissions { get; set; }
}

请尝试使用上面的代码片段。如果有任何问题,请告诉我。

关于c# - 如何在 mvc kendo 网格的 ajax 绑定(bind)中使用客户端模板表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18504490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com