簡體   English   中英

ASP.NET MVC 4 Kendo網格-屬性上的ForeignKeyAttribute無效

[英]ASP.NET MVC 4 Kendo Grid - ForeignKeyAttribute on property is not valid

我試圖在一個表中導航以顯示文本值而不是id,但出現此錯誤:

EntityFramework.dll中發生類型'System.InvalidOperationException'的異常,但未在用戶代碼中處理

附加信息:類型為'DevelopmentNotesProject.Models.NoteForm'的屬性'languageId'上的ForeignKeyAttribute無效。 在依賴類型“ DevelopmentNotesProject.Models.NoteForm”上找不到導航屬性“ Language”。 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; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM