简体   繁体   English

在Kendo UI网格中查看外键

[英]Viewing foreignkey in Kendo UI Grid

I have some problems to navigate from a table to another in my Kendo Grid. 我在Kendo Grid中从一个表导航到另一个表时遇到一些问题。

In my grid, there is a column which is an id and I, of course prefer to display the name instead. 在我的网格中,有一列是id,我当然更喜欢显示名称。 To achieve this, I need to go in the Language Table. 为此,我需要进入“语言表”。

Here is my code: 这是我的代码:

Model: 模型:

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Language> Language { get; set; }
    public DbSet<NoteForm> Note { get; set; }

[Table("Language")]
public class Language
{
    public string lang { get; set; }

    [Key]
    public int id { get; set; }
}
[Table("note")]

public class NoteForm
{
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [Display(Name = "Title")]
    public string Title { get; set; }

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

    [Required]
    [Display(Name = "Language")]
    [ForeignKey("Language")]
    public int languageId { get; set; }

    [Key]
    public int id { get; set; }

    public int userId { get; set; }

}

View : 查看:

@(Html.Kendo().Grid<DevelopmentNotesProject.Models.NoteForm>()
.Name("grid")
.Columns(columns =>
{
    columns.Bound(c => c.Title).Width(200).ClientTemplate(string.Format("{0}...", "#= formatter(Title) #"));
    columns.Bound(c => c.Text).Width(450).ClientTemplate(string.Format("{0}...", "#= formatter(Text) #"));
    columns.ForeignKey(p => p.LanguageId, (System.Collections.IEnumerable)ViewData["Language"], "id", "lang").Title("Language").Width(100);
    columns.Command(command => { command.Edit(); command.Destroy(); });

})
.HtmlAttributes(new { style = "height: 380px;" })
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
    .Refresh(true)
    .PageSizes(true)
    .ButtonCount(5))
 .DataSource(dataSource => dataSource // Configure the grid data source
          .Ajax() // Specify that ajax binding is used
          .Read(read => read.Action("Notes_Read", "MyNotes")) // Set the action method which will return the data in JSON format
          .Destroy(update => update.Action("Notes_Destroy", "MyNotes"))
          .Update(update => update.Action("Notes_Update", "MyNotes"))
          .Model(model => 
              {
                  model.Id(p => p.id);
              })
       )
 .Selectable()
)

In the view code, I tried to write : 在查看代码中,我尝试编写:

  columns.ForeignKey(p => p.Language.id, (System.Collections.IEnumerable)ViewData["Language"], "id", "lang").Title("Language").Width(100);

But i keep get the following error : 但是我一直收到以下错误:

Error 2 Cannot convert lambda expression to type 'string' because it is not a delegate type 错误2无法将lambda表达式转换为类型'string',因为它不是委托类型

need some suggestions and hints on what could be going wrong here and how to resolve this. 需要一些建议和提示,以解决此处可能出现的问题以及如何解决此问题。

I just tried doing that and have no error. 我只是尝试这样做,没有错误。 Which IDE are you using and did you try a clean and rebuild? 您正在使用哪个IDE,并且尝试过清理并重建?

Just thought I'd answer in here in case you want to accept that as an answer. 只是以为我会在这里回答,以防万一您想接受它作为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM