ASP.NET MVC 4 Kendo Grid - 属性上的ForeignKeyAttribute无效

时间:2015-04-29 11:52:37

标签: c# asp.net asp.net-mvc telerik kendo-grid

我正在尝试浏览表格以显示文本值而不是ID,我收到此错误:

  

类型' System.InvalidOperationException'的例外情况发生在   EntityFramework.dll但未在用户代码中处理

     

其他信息:属性上的ForeignKeyAttribute   ' languageId' on type' DevelopmentNotesProject.Models.NoteForm'不是   有效。导航属性'语言'没找到了   依赖类型' DevelopmentNotesProject.Models.NoteForm'。名字   value应该是有效的导航属性名称。

我已经花了几个小时试图解决这个问题是徒劳的...... 非常感谢你的时间 这是我的代码:

查看:

@using Kendo.Mvc.UI
@model DevelopmentNotesProject.Models.NoteForm

@{
    ViewBag.Title = "Index";
}


<script type="text/javascript">
    function formatter(value) {
        return value.substring(0, 50);
    }
</script>


<section id="listing">
    <h2>My Notes</h2>

        @(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["lang"], "Id", "Name").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()
        )



  </section>

@Html.ActionLink("Add a new note", "Add", "MyNotes")

 @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

控制器:

public ActionResult Index()
{
    var dbo = new UsersContext();
    ViewData["lang"] = dbo.Language.Select(b => new { Id = b.id, Name = b.lang });
    return View();
}

型号:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security;

namespace DevelopmentNotesProject.Models
{
    public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Language> Language { get; set; }
        public DbSet<NoteForm> Note { 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; }

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

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

}

1 个答案:

答案 0 :(得分:1)

您需要配置NoteForm和语言模型之间的关系。

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

 [ForeignKey("languageId ")]
 public Language Language { get; set; }