在 Kendo UI 网格中查看外键

Viewing foreignkey in Kendo UI Grid

我在 Kendo 网格中从一个 table 导航到另一个时遇到一些问题。

在我的网格中,有一列是 id,我当然更喜欢显示名称。为此,我需要使用语言 Table.

这是我的代码:

型号:

    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; }

}

查看:

@(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()
)

在视图代码中,我试着写成:

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

但我一直收到以下错误:

Error 2 Cannot convert lambda expression to type 'string' because it is not a delegate type

需要一些建议和提示,了解这里可能出现的问题以及如何解决这个问题。

我刚刚尝试这样做并且没有错误。您正在使用哪个 IDE,您是否尝试过清理和重建?

只是想我会在这里回答,以防你想接受它作为答案。