c# - Viewing data from another model results in DynamicProxies object being shown -
i working on desk system , writing view view tickets user.
a ticket consists of parent data ticket category, open user, technician, etc. , there child ticketnotes. first ticket note (that added upon initial creation) ticket description. in table of tickets on view show 1 note (the ticket description) each ticket. here controller:
public actionresult index() { var model = t in db.tickets join tn in db.ticketnotes on t.ticketid equals tn.ticketid t.openuserid == new guid("00000000-0000-0000-0000-000000000000") orderby t.opendate select t; return view(model); }
my viewmodel:
namespace helpdesk.webui.viewmodel { public class usertickets { public int ticketnumber { get; set; } public guid categoryid { get; set; } public virtual category category { get; set; } public guid openuserid { get; set; } public virtual user openuser { get; set; } public datetime opendate { get; set; } public guid technicianid { get; set; } public virtual user technician { get; set; } public guid ticketstatusid { get; set; } public virtual ticketstatus ticketstatus { get; set; } public nullable<datetime> closedate { get; set; } public string note { get; set; } } }
my models:
namespace helpdesk.model { public class ticket { public ticket() { this.ticketnotes = new hashset<ticketnote>(); } public guid ticketid { get; set; } [databasegenerated(databasegeneratedoption.identity)] [required] public int ticketnumber { get; set; } [foreignkey("category")] [required] public guid categoryid { get; set; } public virtual category category { get; set; } [foreignkey("openuser")] [required] public guid openuserid { get; set; } public virtual user openuser { get; set; } [datatype(datatype.date)] [required] public datetime opendate { get; set; } [foreignkey("technician")] [required] public guid technicianid { get; set; } public virtual user technician { get; set; } [foreignkey("ticketstatus")] [required] public guid ticketstatusid { get; set; } public virtual ticketstatus ticketstatus { get; set; } [datatype(datatype.date)] public nullable<datetime> closedate { get; set; } public virtual icollection<ticketnote> ticketnotes { get; set; } //public virtual icollection<ticketsubscription> ticketsubscriptions { get; set; } } } namespace helpdesk.model { public class ticketnote { public guid ticketnoteid { get; set; } [foreignkey("ticket")] [required] public guid ticketid { get; set; } public virtual ticket ticket { get; set; } public string note { get; set; } [display(name = "attachment")] public string attachmentpath { get; set; } [maxlength(255)] public string attachmentname { get; set; } [foreignkey("usernote")] [required] public guid usernoteid { get; set; } public virtual user usernote { get; set; } [datatype(datatype.date)] [required] public datetime ticketnotedate { get; set; } [display(name = "private note")] [required] public bool publicflag { get; set; } public bool delete { get; set; } } }
and here view:
@model ienumerable<helpdesk.model.ticket> @{ viewbag.title = "index"; } <h2>index</h2> <p> @html.actionlink("create new", "create") </p> <table class="table"> <tr> <th> @html.displaynamefor(model => model.category.categoryname) </th> <th> @html.displaynamefor(model => model.openuser.fullname) </th> <th> @html.displaynamefor(model => model.technician.fullname) </th> <th> @html.displaynamefor(model => model.ticketstatus.statusdescription) </th> <th> @html.displaynamefor(model => model.ticketnumber) </th> <th> @html.displaynamefor(model => model.opendate) </th> <th> @html.displaynamefor(model => model.closedate) </th> <th> @html.displaynamefor(model => model.ticketnotes) </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.category.categoryname) </td> <td> @html.displayfor(modelitem => item.openuser.ntusername) </td> <td> @html.displayfor(modelitem => item.technician.ntusername) </td> <td> @html.displayfor(modelitem => item.ticketstatus.statusdescription) </td> <td> @html.displayfor(modelitem => item.ticketnumber) </td> <td> @html.displayfor(modelitem => item.opendate) </td> <td> @html.displayfor(modelitem => item.closedate) </td> <td> @html.displayfor(modelitem => item.ticketnotes.orderby(t=t.ticketid).first().tickettext) </td> <td> @html.actionlink("edit", "edit", new { id=item.ticketid }) | @html.actionlink("details", "details", new { id=item.ticketid }) | </td> </tr> } </table>
everything works except ticket note pulling ticketnote model. field getting
system.data.entity.dynamicproxies.ticket_ba0bcde55bcbe6ba2fe66fad697c8d2c0d7aad7c5f797406642720cb57da2a89
as suggested 1 changed code in controller be:
public actionresult index() { var model = db.tickets .include(t=>t.ticketnotes) .where(t.openuserid == new guid("00000000-0000-0000-0000-000000000000")) .orderby(t => t.opendate); return view(model); }
and view suggested getting error on line in view:
@html.displayfor(modelitem => item.ticketnotes.orderby(t=t.ticketid).first().tickettext)
it saying 'the name 't' not exist in current context.
(optionally) use in controller better performance eagerly load ticketnotes instead of lazily load them:
public actionresult index() { var model = db.tickets .include(t=>t.ticketnotes) .where(t.openuserid == new guid("00000000-0000-0000-0000-000000000000")) .orderby(t=>t.opendate); return view(model); }
and use in view (which work both eager , lazy loaded notes):
@html.displayfor(modelitem => item.ticketnotes.orderby(t=t.ticketid).first().tickettext)
Comments
Post a Comment