c# - Binding data of Domain model type to ViewModel type -


I have to figure out how to bind data from the database to ViewModel is a difficult time, in fact, I have a The domain model in which I get a lot of assets and which I want to reduce so logically, I have chosen ViewModel to do this.

Domain model (Automatically built database):

  public partial ticket {public ticket () {this.Daily = new HashSet & Lt; Daily & gt; (); This Ticket1 = new hashset & lt; Ticket & gt; (); } Received Public Entity ID Ticket { Set; } Public taps qualified & lt; Int & gt; IdNadredeni tickets {get; Set; } Public RedniBroj {get; Set; } Public Ent Idfirma {Received; Set; } Public taps qualified & lt; Int & gt; IdKontakt {get; Set; } Public taps qualified & lt; Int & gt; Get the IdManager { Set; } Public String Tip {get; Set; } Public string status {get; Set; } Public taps qualified & lt; System.DateTime & gt; Receive datagroups { Set; } Public string VrstaPrijave {get; Set; } Public String Prioritet {get; Set; } Public taps qualified & lt; System.DateTime & gt; Receive DataModo {Get; Set; } Public string Opis {get; Set; } Public string Biljeske {get; Set; } Public Nullable & lt; Bool & gt; Zatourne {get; Set; } Public Nullable & lt; Bool & gt; IzdanRacun {Receive; Set; } Public taps qualified & lt; System.DateTime & gt; Statistical description {get; Set; } Public taps qualified & lt; Int & gt; IDSet {Receive; Set; } Public Virtual Icings & Lt; Daily & gt; Daily {Received; Set; } Public virtual firm form {receipt; Set; } Receive public virtual contact {receipt; Set; } Public Virtual Contact Kontakt1 {get; Set; } Public Virtual Iconsing & amp; Ticket; Ticket & gt; Ticket 1 {Get; Set; } Received {2} Public Virtual Ticket Ticket 2; Set; }}  

see MODEL:

  public class opencatalogs {public entit ID ticket {received; Set; } Public taps qualified & lt; Int & gt; IdNadredeni tickets {get; Set; } Public RedniBroj {get; Set; } Public Ent Idfirma {Received; Set; } Public taps qualified & lt; Int & gt; IdKontakt {get; Set; } Public taps qualified & lt; Int & gt; Get the IdManager { Set; } Public String Tip {get; Set; } Public string status {get; Set; } Public taps qualified & lt; System.DateTime & gt; Receive datagroups { Set; } Public string VrstaPrijave {get; Set; } Public String Prioritet {get; Set; } Public string Opis {get; Set; } Public string Biljeske {get; Set; } Public String Brositeta {Mill {return ID Nadenney ticket. Toastring () + "-" + redneckbrose. Toasting (); }} Public String NazivTicketa {get {return BrojTicketa + "-" + opis; }} Public string DetaljiTicketa {get {return Opis + "\ r \ n" + Biljeske; }}}  

What I want to do is to force the data from the database to be viewed on the ViewModel, but wisely, I get errors about various types of objects in the view . I am posting the controller and looking for reference.

Controller

  Public Performance Opencounts () {var openTickets = db.Ticket. (T = & gt; t.idFirma == 1). Where (t = & gt; tjetvoran == wrong); See Return (openTickets.ToList ()); }  

(Some codes are deliberately omitted for short)

  @modelimimableable & lt; IDE3_CRM.ViewModels.OpenTickets & gt; & Lt; Table & gt; @forcha (Miscellaneous items in the model) {& lt; Tr & gt; & Lt; Td> @ Html.DisplayFor (ModelEystem => Items. Bill's) & lt; / Td> & Lt; Td> @ Html.DisplayFor (modelItem = & gt; item.Opis) & lt; / Td> & Lt; / TR & gt; } & Lt; / Table & gt;    

I recommend wrapping your DB call in a repository Give it Here, you can turn your database object into a visual model. For example:

  public performance opencartes () {var opencatalogs = ticket repayments.get opencartes (); View return (opentalks); } // ITTRPO FIC INIMERABLE and LT; OpenTX & gt; The implementation of GetOpenTickets () {return db.Ticket. Where (t = & gt; t.idFirma == 1 & amp; amp; amp;; tzatvoren == wrong). Select (View => New Properties} // Model Properties View Database Database}}; }  

Comments