ASP.NET MVC 4 Kendo 网格 - 属性 上的 ForeignKeyAttribute 无效
ASP.NET MVC 4 Kendo Grid - ForeignKeyAttribute on property is not valid
我试图在 table 中导航以显示文本值而不是 ID,但我收到此错误:
An exception of type 'System.InvalidOperationException' occurred in
EntityFramework.dll but was not handled in user code
Additional information: The ForeignKeyAttribute on property
'languageId' on type 'DevelopmentNotesProject.Models.NoteForm' is not
valid. The navigation property 'Language' was not found on the
dependent type 'DevelopmentNotesProject.Models.NoteForm'. The Name
value should be a valid navigation property name.
几个小时以来,我一直在努力解决这个问题,但徒劳无功...
非常感谢你的时间
这是我的代码:
查看:
@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; }
}
}
您需要配置NoteForm与语言模型的关系。
[Required]
[Display(Name = "Language")]
public int languageId { get; set; }
[ForeignKey("languageId ")]
public Language Language { get; set; }
我试图在 table 中导航以显示文本值而不是 ID,但我收到此错误:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The ForeignKeyAttribute on property 'languageId' on type 'DevelopmentNotesProject.Models.NoteForm' is not valid. The navigation property 'Language' was not found on the dependent type 'DevelopmentNotesProject.Models.NoteForm'. The Name value should be a valid navigation property name.
几个小时以来,我一直在努力解决这个问题,但徒劳无功... 非常感谢你的时间 这是我的代码:
查看:
@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; }
}
}
您需要配置NoteForm与语言模型的关系。
[Required]
[Display(Name = "Language")]
public int languageId { get; set; }
[ForeignKey("languageId ")]
public Language Language { get; set; }