Tuesday, 15 June 2010

c# - Display list of related entities in MVC view -



c# - Display list of related entities in MVC view -

i have table ecmain has relationship 2 other tables, notes , email.

i want access info in view list of toes appear every record in ecmain , editable.

here model

namespace editsuite.models { public class ecmain { public ecmain() { this.notes = new collection<notes>(); } public int id { get; set; } public string auth { get; set; } public string keywords { get; set; } public string description { get; set; } public string url { get; set; } public string category { get; set; } public string subcategory { get; set; } public string title { get; set; } public string live { get; set; } public virtual icollection<notes> notes { get; set; } public virtual icollection<email> email { get; set; } public list<notes> notelist { get; set; } } public class email { public int id { get; set; } public int ecmainid { get; set; } public string emailtext { get; set; } public virtual ecmain ecmain { get; set; } } public class notes { public int id { get; set; } public int ecmainid { get; set; } public string notestext { get; set; } public virtual ecmain ecmain { get; set; } }

db context

namespace editsuite.dal { public class eccontext : dbcontext { public eccontext() : base("name=ecmodelconnection") { } public virtual dbset<ecmain> ecmain { get; set; } public virtual dbset<email> email { get; set; } public virtual dbset<notes> notes { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<pluralizingtablenameconvention>(); } } }

controller

using system; using system.collections.generic; using system.data; using system.data.entity; using system.linq; using system.net; using system.web; using system.web.mvc; using editsuite.dal; using editsuite.models; namespace editsuite.controllers { public class ecmainscontroller : controller { private eccontext db = new eccontext(); // get: ecmain public actionresult index() { //var church = m in db.ecmain.take(10) // m.live == "y" && m.auth == "yes" // select m; //church = church.orderby(m => m.category); homecoming view(db.ecmain.take(10)); }

view

@foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.notes) </td> <td> @html.displayfor(modelitem => item.keywords) </td> <td> @html.displayfor(modelitem => item.description) </td> <td> @html.displayfor(modelitem => item.url) </td> <td> @html.displayfor(modelitem => item.category) </td> <td> @html.displayfor(modelitem => item.subcategory) </td> <td> @html.displayfor(modelitem => item.title) </td> <td> @html.displayfor(modelitem => item.live) </td> <td> @html.actionlink("edit", "edit", new { id=item.id }) | @html.actionlink("details", "details", new { id=item.id }) | @html.actionlink("delete", "delete", new { id=item.id }) </td> </tr> }

the result server error in '/' application.

invalid column name 'ecmain_id1'. invalid column name 'ecmain_id1'. invalid column name 'ecmain_id'. invalid column name 'ecmain_id1'.

description: unhandled exception occurred during execution of current web request. please review stack trace more info error , originated in code.

exception details: system.data.sqlclient.sqlexception: invalid column name 'ecmain_id1'. invalid column name 'ecmain_id1'. invalid column name 'ecmain_id'. invalid column name 'ecmain_id1'

i tried @html.displayfor(modelitem => item.notelist) returned nothing

this sounds lot problem: asp.net mvc /entity framework error - invalid column name 'environment_id'

the reply there may helpful you.

gerry

3rd party edit

for me reply linked gerry of utilize problem. have not worked much ef , mvc based on exception system.data.sqlclient.sqlexception: invalid column name 'ecmain_id1' or 'ecmain_id' assume

that sql statement created entity-framework not match columns in database or matching between table-columns sql-result , properties of email-object fails

c# asp.net-mvc entity-framework-4.1

No comments:

Post a Comment