Upload a Grid in a view listing individuals and legal entities - Asp.net core MVC

0

I need to load a Grid in a view listing physical and legal entities, but I'm having difficulty because they are in different tables. I'll explain better:

1 - I have the classes Person, Person and PersonJuridica.

public class Pessoa : Entity
    {
        public virtual PessoaNatureza PessoaNatureza { get; private set; }
        public virtual PessoaFisica PessoaFisica { get; private set; }
        public virtual PessoaJuridica PessoaJuridica { get; private set; }
        public virtual ICollection<PessoaGenerico> PessoaGenerico { get; private set; }
        public virtual Filial Filial { get; private set; }


        protected Pessoa() { }       

        public Pessoa(int id, PessoaFisica pessoaFisica)
        {            
            Id = id;
            PessoaNatureza = ValueObjects.PessoaNatureza.Física;
            PessoaFisica = pessoaFisica;
        }

        public Pessoa(int id, PessoaJuridica pessoaJuridica)
        {
            Id = id;
            PessoaNatureza = ValueObjects.PessoaNatureza.Jurídica;
            PessoaJuridica = pessoaJuridica;
        }
    }

public class PessoaFisica : Entity
    {
        public string NomeCompleto { get; private set; }
        public string Apelido { get; private set; }
        public DateTime DataNascimento { get; private set; }

        public virtual Pessoa Pessoa { get; private set; }
        public virtual Sexo Sexo { get; private set; }
        public virtual EstadoCivil EstadoCivil { get; private set; }

        protected PessoaFisica() { }

        public PessoaFisica(int id, string nomeCompleto, string apelido, DateTime dataNascimento, Sexo sexo, EstadoCivil estadoCivil)
        {
            Id = id;
            NomeCompleto = nomeCompleto;
            Apelido = apelido;
            DataNascimento = dataNascimento;
            Sexo = sexo;
            EstadoCivil = estadoCivil;
        }
    }

public class PessoaJuridica : Entity
    {
        public string RazaoSocial { get; private set; }
        public string NomeFantasia { get; private set; }
        public DateTime DataAbertura { get; private set; }

        public virtual Pessoa Pessoa { get; private set; }

        protected PessoaJuridica() { }

        public PessoaJuridica(int id, string razaoSocial, string nomeFantasia, DateTime dataAbertura)
        {
            Id = id;
            RazaoSocial = razaoSocial;
            NomeFantasia = nomeFantasia;
            DataAbertura = dataAbertura;
        }
    }

I chose the Aggregation relationship, so the relationship between the 3 tables will be one-to-one. A Person can be Physical or Legal and to ensure this, in my repository, I have created a method that will "include" and return me specifically records.

 public IQueryable<Pessoa> GetAllJoin()
        {
            return DbSet.Where(p => p.PessoaNatureza == PessoaNatureza.Física)
            .Include("PessoaFisica")
            .Concat(
                   DbSet.Where(p => p.PessoaNatureza == PessoaNatureza.Jurídica)
                        .Include("PessoaJuridica")
            ).AsNoTracking();
        }

So far, everything works harmonically ... Now we're going to Grid rsrsrs. I need to mount a Grid to show me the records that are in the 3 tables:

Person (PersonId, PersonNature) + PersonPhysical / Juridical (Name / Race, Surname / Fantasy, Birth / Opening)

Example: - 1 - Physics - Pedro Santos - Pedrinho - 09/13/1980 - 2 - Legal - Supermarket Acme- Acme e Filhos Ltda - 12-09-1987

Now I do not know how to do it .... How should I mount my viewmodel? How should my view be? Would it be necessary to change my GetJoinAll method?

 public class PessoaViewModel
    {
        [Key]
        [DisplayName("Código")]
        public int Id { get; set; }

        [Required(ErrorMessage = "O campo Natureza da Pessoa é requerido")]
        [DisplayName("Natureza")]
        public PessoaNatureza PessoaNatureza { get; set; }


    }

@model IEnumerable<SistemaComercial.Application.ViewModels.PessoaViewModel>
@{
    ViewData["Title"] = "Gerenciar Pessoas";
}
<div>
    <h2>@ViewData["Title"]</h2>
</div>
<hr />
<div class="row">
    <div class="col-md-12">
        <div>
            <div class="pull-left">
                <a asp-action="Create" class="btn btn-primary">
                    <span title="Register New" class="glyphicon glyphicon-plus-sign"></span> Register New
                </a>
            </div>
        </div>
    </div>
</div>
<br />
<div class="panel panel-default">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.PessoaNatureza)
                </th>                
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PessoaNatureza)
                </td>                
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id" title="Edit" class="btn btn-warning">
                        <span class="glyphicon glyphicon-pencil"></span>
                    </a>

                    <a asp-action="Details" asp-route-id="@item.Id" title="Details" class="btn btn-primary">
                        <span class="glyphicon glyphicon-search"></span>
                    </a>

                    <a asp-action="Delete" asp-route-id="@item.Id" title="Delete" class="btn btn-danger">
                        <span class="glyphicon glyphicon-trash"></span>
                    </a>

                    <button type="button" class="btn btn-purple viewbutton" title="History" data-id="@item.Id" data-toggle="modal" data-target="#customerHistory">
                        <span class="glyphicon glyphicon-time"></span>
                    </button>
                </td>
            </tr>
            }
        </tbody>
    </table>
</div>
<div class="modal fade" id="customerHistory" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Customer Data History</h4>
            </div>
            <div class="modal-body">
                <p id="customerHistoryData"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
@section scripts
    {
    <script type="text/javascript">
        $(".viewbutton").on("click", function() {
            var customerId = $(this).data('id');
            $.ajax({
                url: "http://localhost:52346/customer-management/customer-history/" + customerId,
                //url: "http://equinoxproject.azurewebsites.net/customer-management/customer-history/" + customerId,
                cache: false
            }).done(function(data) {
                var formatHtml = "<table class='table table-striped'>";
                formatHtml += "<thead><th>Action</th><th>When</th><th>Id</th><th>Name</th><th>E-mail</th><th>Birth Date</th><th>By User</th></thead>";

                for (var i = 0; i < data.length; i++) {
                    var change = data[i];
                    formatHtml += "<tr>";
                    formatHtml += "<td>" + change.action + "</td>";
                    formatHtml += "<td>" + change.when + "</td>";
                    formatHtml += "<td>" + change.id + "</td>";
                    formatHtml += "<td>" + change.name + "</td>";
                    formatHtml += "<td>" + change.email + "</td>";
                    formatHtml += "<td>" + change.birthDate + "</td>";
                    formatHtml += "<td>" + change.who + "</td>";
                    formatHtml += "</tr>";
                }
                formatHtml += "</table>";
                $("#customerHistoryData").html(formatHtml);
            });
        });
    </script>
}
    
asked by anonymous 25.02.2018 / 12:21

0 answers