c# - ASP.NET MVC 4 Kendo Grid - ForeignKeyAttribute on property is not valid -


i trying navigate through table in order display text value instead of id , error:

an exception of type 'system.invalidoperationexception' occurred in entityframework.dll not handled in user code

additional information: foreignkeyattribute on property 'languageid' on type 'developmentnotesproject.models.noteform' not valid. navigation property 'language' not found on dependent type 'developmentnotesproject.models.noteform'. name value should valid navigation property name.

it's been hours i've been trying solve problem in vain ... lot time code:

view:

@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 grid data source                   .ajax() // specify ajax binding used                   .read(read => read.action("notes_read", "mynotes")) // set action method return 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 new note", "add", "mynotes")   @section scripts {         @scripts.render("~/bundles/jqueryval")     } 

controller:

public actionresult index() {     var dbo = new userscontext();     viewdata["lang"] = dbo.language.select(b => new { id = b.id, name = b.lang });     return view(); } 

model:

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

you need configure relationship between noteform , language models.

 [required]  [display(name = "language")]  public int languageid { get; set; }   [foreignkey("languageid ")]  public language language { get; set; } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -