简体   繁体   中英

Viewing foreignkey in Kendo UI Grid

I have some problems to navigate from a table to another in my Kendo Grid.

In my grid, there is a column which is an id and I, of course prefer to display the name instead. 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

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?

Just thought I'd answer in here in case you want to accept that as an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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