2014-10-27 55 views
0

我很難弄清楚如何將數據從數據庫綁定到ViewModel。基本上,我有一個領域模型,我發現有太多的屬性,我想減少,所以邏輯上我選擇了ViewModel來做到這一點。將域模型類型的數據綁定到ViewModel類型

領域模型(從數據庫中自動創建):

public partial class Ticket 
{ 
    public Ticket() 
    { 
     this.Daily = new HashSet<Daily>(); 
     this.Ticket1 = new HashSet<Ticket>(); 
    } 

    public int idTicket { get; set; } 
    public Nullable<int> idNadredeniTicket { get; set; } 
    public short RedniBroj { get; set; } 
    public int idFirma { get; set; } 
    public Nullable<int> idKontakt { get; set; } 
    public Nullable<int> idManager { get; set; } 
    public string Tip { get; set; } 
    public string Status { get; set; } 
    public Nullable<System.DateTime> DatumPrijave { get; set; } 
    public string VrstaPrijave { get; set; } 
    public string Prioritet { get; set; } 
    public Nullable<System.DateTime> DatumDo { get; set; } 
    public string Opis { get; set; } 
    public string Biljeske { get; set; } 
    public Nullable<bool> Zatvoren { get; set; } 
    public Nullable<bool> IzdanRacun { get; set; } 
    public Nullable<System.DateTime> DatumZatvaranja { get; set; } 
    public Nullable<int> idAsset { get; set; } 

    public virtual ICollection<Daily> Daily { get; set; } 
    public virtual Firma Firma { get; set; } 
    public virtual Kontakt Kontakt { get; set; } 
    public virtual Kontakt Kontakt1 { get; set; } 
    public virtual ICollection<Ticket> Ticket1 { get; set; } 
    public virtual Ticket Ticket2 { get; set; } 
} 

視圖模型:

public class OpenTickets 
{ 
    public int idTicket { get; set; } 
    public Nullable<int> idNadredeniTicket { get; set; } 
    public short RedniBroj { get; set; } 
    public int idFirma { get; set; } 
    public Nullable<int> idKontakt { get; set; } 
    public Nullable<int> idManager { get; set; } 
    public string Tip { get; set; } 
    public string Status { get; set; } 
    public Nullable<System.DateTime> DatumPrijave { get; set; } 
    public string VrstaPrijave { get; set; } 
    public string Prioritet { get; set; } 
    public string Opis { get; set; } 
    public string Biljeske { get; set; } 

    public string BrojTicketa 
    { 
     get 
     { 
      return idNadredeniTicket.ToString() + "-" + RedniBroj.ToString(); 
     } 
    } 
    public string NazivTicketa 
    { 
     get 
     { 
      return BrojTicketa + " - " + Opis; 
     } 
    } 
    public string DetaljiTicketa 
    { 
     get 
     { 
      return Opis + "\r\n" + Biljeske; 
     } 
    } 
} 

我想做到的是從通過查詢數據庫中的數據綁定到ViewModel,但是,理解,我得到關於傳遞給視圖的不同類型的對象的錯誤。我正在發佈控制器和視圖以供參考。

控制器

public ActionResult OpenTickets() 
    { 
     var openTickets = db.Ticket 
      .Where(t => t.idFirma == 1) 
      .Where(t => t.Zatvoren == false); 

     return View(openTickets.ToList()); 
    } 

視圖(一些代碼故意中省略爲簡潔起見)

@model IEnumerable<IDE3_CRM.ViewModels.OpenTickets> 

<table> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td>@Html.DisplayFor(modelItem => item.Biljeske)</td> 
      <td>@Html.DisplayFor(modelItem => item.Opis)</td> 
     </tr> 
    } 
</table> 

回答

1

我建議在Repository包裹你的數據庫調用。在這裏,您可以將數據庫對象轉換爲視圖模型。例如:

public ActionResult OpenTickets() 
{ 
    var openTickets = ticketRepo.GetOpenTickets(); 
    return View(openTickets); 
} 

// Implementation of ITicketRepo 
public IEnumerable<OpenTickets> GetOpenTickets() 
{ 
    return db.Ticket 
     .Where(t => t.idFirma == 1 && t.Zatvoren == false) 
     .Select(do => new OpenTickets 
     { 
      // Fill in view model properties from database object 
     }); 
} 
+0

非常好!謝謝,還有一件事。看來OpenTicket.cs中的自定義屬性無法識別,例如BrojTicketa,NazivTicketa,DetaljiTicketa。有關於此的任何信息? @beautifulcoder – 2014-10-27 19:30:56

+0

這些是隻讀字段,它們只實現'get' – beautifulcoder 2014-10-27 22:06:19

+0

謝謝,通過添加'set',我們發現它們顯示爲我所期望的。 – 2014-10-27 22:29:05

1

Hrvach,

您可以限制在視圖本身的數據字段,我想,也許更有效。話雖這麼說這裏是另一種方法,你可以採取:

  1. 創建類型OpenTickets列表
  2. 選擇要
  3. 循環票在選定的門票,並添加新openTicket你想要的 性質在保持到openTickets名單
  4. 回程OPEN票

    public ActionResult OpenTickets() 
    { 
    
    
        List<OpenTickets> openTicketList = new List<OpenTickets>();//create a list of openTickets 
    
        var Tickets = db.Ticket//select the tickets that you want 
         .Where(t => t.idFirma == 1) 
         .Where(t => t.Zatvoren == false); 
    
        foreach (var ticket in Tickets)//Loop over the tickets and create an openTicket out of each ticket then add the openTick to the openTicketList 
        { 
         OpenTickets openTicket = new OpenTickets();//create new OpenTickets object 
         openTicket.propery1 = ticket.propery1;//set each property of the openTicket equal to the property of the Ticket that you want to keep 
         openTicket.propery2 = ticket.propery2; 
         openTicket.propery3 = ticket.propery3; 
         openTicket.propery4 = ticket.propery4; 
         openTicketList.Add(openTicket);//add new OpenTickets object to the list 
        } 
        return View(openTicketList); 
    } 
    
名單3210

我希望這可以幫助...最好的祝願 條例草案

+0

這非常有幫助,並且像下面的答案一樣也是答案,但是,我更喜歡@beautifulcoders回答你的一件事;它會自動提供屬性。再一次感謝你! – 2014-10-27 19:19:31