Asp.net mvc 4 剑道网格外键模板

Asp.net mvc 4 剑道网格外键模板,asp.net-mvc-4,kendo-grid,Asp.net Mvc 4,Kendo Grid,我的剑道网格中有一个外键,我已经为这个外键创建了一个编辑器。它在保存时工作正常,但问题是当网格显示数据时,外键值未定义。我知道我必须更改模板以显示正确的值。我添加了函数intemplate,以显示正确的值,但它对我不起作用 你能帮我吗?这是我的密码: var collection; $("#mygrid").kendoGrid({ dataSource: dataSource, pageable: true, toolbar: [{ name: 'create',

我的剑道网格中有一个外键,我已经为这个外键创建了一个编辑器。它在保存时工作正常,但问题是当网格显示数据时,外键值未定义。我知道我必须更改模板以显示正确的值。我添加了函数
intemplate
,以显示正确的值,但它对我不起作用

你能帮我吗?这是我的密码:

var collection;

$("#mygrid").kendoGrid({
    dataSource: dataSource,
    pageable: true,  
    toolbar: [{ name: 'create', text: 'My create' }],
    columns: [
        { field: "ForeignKeyColumn", editor: categoryDropDownEditor, template: "#=getTextByValue(data)#" },
        { field: "NOTES", title: "Notes" },
        { command: ["edit", "destroy"], title: " ", width: "160px" }],
    editable: "popup",                    
});

//update model when choose value from dropdown list
var grid = $("#mygrid").data("kendoGrid");
grid.bind("save", grid_save);
function grid_save(e) {

    if (e.model.ForeignKey) {

        //change the model value
        e.model.ForeignKeyColumn = 0;
        //get the currently selected value from the DDL
        var currentlySelectedValue = $(e.container.find("#typeCombo")[0]).data().kendoDropDownList.value();
        //set the value to the model
        e.model.set('ForeignKeyColumn', currentlySelectedValue);            
    }
}

//Editor template
function categoryDropDownEditor(container, options) {
    $('<input id="typeCombo" required data-text-field="text" data-value-field="value" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: true,
            dataSource: {
                type: "json",
                transport: {
                    read: {
                        url: '@Url.Action("SomeAction", "SomeController")',
                        type: "POST",
                        contentType: "application/json",
                    }
                }
            }
        });
}

//Show template
function getTextByValue(data) {
    //if the collection is empty - get it from the grid
    if (!collection) {
        grid = $("#GridName").data("kendoGrid");
         valuesCollection = grid.options.columns[1].values;//Set the correct FKColumn index
        collection = {};           
        for (var value in valuesCollection) {
            collection[valuesCollection[value].value] = valuesCollection[value].text;
        }
    }
    return collection[data.ForeignKeyColumn];
}
var集合;
$(“#我的网格”).kendoGrid({
数据源:数据源,
pageable:对,
工具栏:[{name:'create',text:'My create'}],
栏目:[
{字段:“ForeignKeyColumn”,编辑器:CategorHydropodowneditor,模板:“#=getTextByValue(数据)#”},
{字段:“注释”,标题:“注释”},
{命令:[“编辑”、“销毁”],标题:,宽度:“160px”},
可编辑:“弹出”,
});
//从下拉列表中选择值时更新模型
var grid=$(“#mygrid”).data(“kendoGrid”);
绑定(“保存”,grid\u save);
功能网格保存(e){
if(例如型号外键){
//更改模型值
e、 model.ForeignKeyColumn=0;
//从DDL中获取当前选定的值
var currentlySelectedValue=$(e.container.find(“#typeCombo”)[0]).data().kendoDropDownList.value();
//设置模型的值
e、 model.set('ForeignKeyColumn',currentlySelectedValue);
}
}
//编辑器模板
函数类别HydropDownEditor(容器、选项){
$('')
.appendTo(容器)
.kendoDropDownList({
自动绑定:是的,
数据源:{
键入:“json”,
运输:{
阅读:{
url:'@url.Action(“SomeAction”、“SomeController”),
类型:“POST”,
contentType:“应用程序/json”,
}
}
}
});
}
//显示模板
函数getTextByValue(数据){
//如果集合为空-从网格中获取它
如果(!集合){
grid=$(“#GridName”).data(“kendoGrid”);
valuesCollection=grid.options.columns[1].values;//设置正确的列索引
集合={};
for(VALUES集合中的var值){
集合[valuesCollection[value]=valuesCollection[value].text;
}
}
返回集合[data.ForeignKeyColumn];
}
注意:
valuesCollection
value在我跟踪时未定义。

试试这个

我认为在网格中绑定外键列是必要的。请不要更改.chtml视图名称和文件夹名称

储存在下面的位置。 位置:-YourViews->Shred->EditorTemplates->GridForeignKey.cshtml

GridForeignKey.cshtml
@(
 Html.Kendo().DropDownListFor(m => m)
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(""))
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
             .OptionLabel("--Not Category--")      
)

您尝试过在EditorTemplates中创建“GridForeignKey”吗?没有,有任何有用的链接吗?如果您已将选项标签传递到外键定义中,则此处不需要它。谢谢你。我几乎一整天都在为这个问题绞尽脑汁。如果我想要一个非通用的选项标签,我该如何将它传递给GridForeignKey呢?