Is it more correct to use the Master-Detail concept?

Asked

Viewed 104 times

1

I have a situation that I would like to use the most correct (elegant) way to implement. The scenario is as follows:

I’m building a Helpdesk system where Tickets are stored in one table (and consequently have their own Model) and Ticket Evolutions are stored in another.

The implementation I’m doing is with C# using MVC and Razor

I’ve already done the Viewticket listing and clicking on it I go to another that presents details with information of the title, content, sector that registered and user.

I’d like to bring along this View details the history of evolutions (and even register new evolutions related to the ticket) and it is precisely in this that I am having difficulties (gather in a single View information from model ticket and the model developments).

Below are the structures assembled for better understanding: MODELS

public class ticket
{
    [Key]
    public int id_ticket { get; set; }
    public int id_setor { get; set; }
    public int id_usuario { get; set; }
    public string assunto_ticket { get; set; }
    public string prioridade_ticket { get; set; }
    public string mensagem_ticket { get; set; }
    public DateTime data_ticket { get; set; }
    public string status_ticket { get; set; }
}


public class Evolucao
{
    [Key]
    public int id_evolucao { get; set; }
    public int id_ticket { get; set; }
    public int id_usuario { get; set; }
    public string texto_evolucao { get; set; }
    public DateTime data_evolucao { get; set; }
}
  • I believe that the relationship between models remains. Type in Evolucao public virtual ticket id_ticket { get; set; }

  • another suggestion use the standard name pattern already standardized for models. instead of id_evolution the correct one would be Evolucaoid so you won’t need to use [Foreignkey("id_ticket")] in the models' relations, nor [Key] because with the Id ending he already knows that he is Key

1 answer

0

Marcelo you can use Viewmodel Pattern.

In the case of using information from two Models in the same view you can create a Viewmodel that contains the two or more Models.

Viewmodel should have no methods, only properties needed for the view. Your view will work with this Viewmodel and your controller should do all the work of loading/downloading this Viewmodel with the Models data.

If I understood your example, each ticket can have multiple evolutions, then your Viewmodel should load the ticket in question and a collection of your evolutions.

    public class EvolucaoDoTicketViewModel
{
    public Ticket Ticket { get; set; }
    public IEnumerable<Evolucao> Evolucao { get; set; }
}

In your controller you must load the ticket in question and the collection with the evolutions related to your id.

You can work with this data in your view and then in the controller (in case of modifying the data for example) "download" the Viewmodel data in the corresponding models and save them.

  • Hello Fabri, I made the implementation through the use of view model as suggested, but I’m having difficulty in presenting the data of the evolutions in the View. All I could do was use the Gridview helper. Do you know any way I can use HTML Helpers to display the information contained in the Evolutions collection in Viewmodel?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.