Search code examples
c#asp.net-mvcasp.net-mvc-viewmodel

Binding data of Domain model type to ViewModel type


I'm having a hard time trying to figure out how to bind data from the database to ViewModel. Basically, I have a domain model which I find has too much properties and which I'd like to reduce so logically I've chosen ViewModel to do so.

Domain model (automatically created from the database):

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

ViewModel:

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

What I'd like to accomplish is to bind data from the database via query to the ViewModel but, understandingly, I get errors regarding different types of objects passed to the View. I'm posting controller and view for the reference.

Controller

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

        return View(openTickets.ToList());
    }

View (some code is intentionally ommited for brevity)

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

Solution

  • I recommend wrapping your db calls in a Repository. In here, you can transform your database objects into view models. For example:

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