locked
I Have problem in ICollection ASP CORE RRS feed

  • Question

  • User419418919 posted

    Good morning, I have a question about something I am trying to develop, the project is in ASP CORE 3.1 MVC. I have 2 identities one to one and the other (which is the one with problems) from one to many. What happens is that I am adding a company (Company Model) and those responsible for this company (Company Responsable Model) which are the following:

        public partial class Empresa
        {
    
            public int EmpresaId { get; set; }
            public string NombreEmpresa { get; set; }
            public string NumeroDistribuidor { get; set; }
            public string Ubicacion { get; set; }
            public string RazonSocial { get; set; }
            public string Domicilio { get; set; }
            public string Telefono { get; set; }
            public string Correo { get; set; }
            public int CantidadEmpleados { get; set; }
    
            public string FechaAlta { get; set; }
            public string HoraAlta { get; set; }
            public string FechaModificacion { get; set; }
            public string HoraModificacion { get; set; }
            public string UsuarioAlta { get; set; }
            public string UsuarioModificacion { get; set; }
            public bool? Estado { get; set; }
    
            public virtual NaturalezaEmpresa NaturalezaEmpresa { get; set; }
            public ICollection<ResponsableEmpresa> ResponsableEmpresas { get; set; }
        }
    }

    Model ResponsableEmpresa:

            public int ResponsableEmpresaId { get; set; }
            public string Puesto { get; set; }
            public string Nombre { get; set; }
            public string ApellidoMaterno { get; set; }
            public string ApellidoPaterno { get; set; }
            public string Correo { get; set; }
            public string Telefono { get; set; }
    
            [ForeignKey("Empresa")]
            public int EmpresaId { get; set; }
    
            public virtual Empresa Empresa { get; set; }

    Now, what I try is that in the view I can add the data of the company and separate those responsible, which would be the following view:

    @model Empresa
    
    <h1 class="text-center">Datos generales de la empresa o grupo</h1>
    
    <form asp-action="Guardar" asp-controller="Empresa" method="post">
    
        <div class="form-group col">
            <label>Nombre de la empresa:</label>
            <input type="text" asp-for="NombreEmpresa" class="form-control" />
            <span asp-validation-for="NombreEmpresa" class="text-danger"></span>
        </div>
        <div class="form-group col">
            <label>Número de distribuidor:</label>
            <input type="text" asp-for="NumeroDistribuidor" class="form-control" />
            <span asp-validation-for="NumeroDistribuidor" class="text-danger"></span>
        </div>
        <div class="form-group col">
            <label>Ubicación:</label>
            <input type="text" asp-for="Ubicacion" class="form-control" />
            <span asp-validation-for="Ubicacion" class="text-danger"></span>
        </div>
        <div class="form-group col">
            <label>Razón Social:</label>
            <input type="text" asp-for="RazonSocial" class="form-control" />
            <span asp-validation-for="RazonSocial" class="text-danger"></span>
        </div>
        <div class="form-group col">
            <label>Domicilio:</label>
            <input type="text" asp-for="Domicilio" class="form-control" />
            <span asp-validation-for="Domicilio" class="text-danger"></span>
        </div>
        <div class="form-group col">
            <label>Telefono: </label>
            <input type="tel" asp-for="Telefono" class="form-control" />
            <span asp-validation-for="Telefono" class="text-danger"></span>
        </div>
        <div class="form-group col">
            <label>Correo: </label>
            <input type="email" asp-for="Correo" class="form-control" />
            <span asp-validation-for="Correo" class="text-danger"></span>
        </div>
    
        <div class="form-group col">
            <label>Cantidad de empleados: </label>
            <input type="number" asp-for="CantidadEmpleados" class="form-control" />
            <span asp-validation-for="CantidadEmpleados" class="text-danger"></span>
        </div>
    
        <div class="form-group col">
            <label>Cantidad de empleados: </label>
            <input asp-for="ResponsableEmpresas.Add(@responsableEmpresa)" class="form-control" />
        </div>
    
    
        @*<h1 class="text-center">Responsable Empresa</h1>
            <p><br /><br /></p>
    
    
            <table id="myTable" class="table table-bordered text-center">
                <thead>
                    <tr>
                        <td scop="col">Puesto</td>
                        <td scop="col">Nombre</td>
                        <td scop="col">Apellido Materno</td>
                        <td scop="col">Apellido Paterno</td>
                        <td scop="col">Correo</td>
                        <td scop="col">Telefono</td>
                    </tr>
                </thead>
    
                <tbody>
                    <tr>
                    </tr>
                </tbody>
            </table>
            <br>
    
            <script>
                function myFunction() {
                    var table = document.getElementById("myTable");
                    table.contentEditable = true;
                    var row = table.insertRow(2);
                    var cell1 = row.insertCell(0);
                    var cell2 = row.insertCell(1);
                    var cell3 = row.insertCell(2);
                    var cell4 = row.insertCell(3);
                    var cell5 = row.insertCell(4);
                    var cell6 = row.insertCell(5);
                    cell1.innerHTML = "Ingresar Puesto";
                    cell2.innerHTML = "Ingresar Nombre";
                    cell3.innerHTML = "Ingresar Apellido Materno";
                    cell4.innerHTML = "Ingresar Apellido Paterno";
                    cell5.innerHTML = "Ingresar Correo ";
                    cell6.innerHTML = "Ingresar Teléfono";
    
                }
                function myDeleteFunction() {
                    document.getElementById("myTable").deleteRow(2);
                }
    
    
                function showTableData() {
                    //gets table
                    var oTable = document.getElementById('myTable');
                    //gets rows of table
                    var rowLength = oTable.rows.length;
                    //loops through rows
                    for (i = 0; i < rowLength; i++) {
                        //gets cells of current row
                        var oCells = oTable.rows.item(i).cells;
                        //gets amount of cells of current row
                        var cellLength = oCells.length;
                        //loops through each cell in current row
                        for (var j = 0; j < cellLength; j++) {
                            // get your cell info here
                            var cellVal = oCells.item(j).innerHTML;
                            if (cellVal.toString() != "Puesto" && cellVal.toString() != "Nombre" && cellVal.toString() != "Nombre" && cellVal.toString() != "Apellido Materno" && cellVal.toString() != "Apellido Paterno" && cellVal.toString() != "Correo" && cellVal.toString() != "Telefono") {
                                alert(cellVal);
                            }
                        }
                    }
                }
    
    
            </script>*@
    
    
        <div class="row float-right" style="margin: 10px 5px 5px 5px">
            <a>
                <input type="button" onclick="myFunction()" class="btn btn-info float-right" value="Insertar" />
            </a>
        </div>
    
        <div class="row float-right" style="margin: 10px 5px 5px 5px">
            <a>
                <input type="button" onclick="myDeleteFunction()" class="btn btn-danger float-right" value="Borrar" />
            </a>
        </div>
    
        <div class="row float-right" style="margin: 10px 5px 5px 5px">
            <a>
                <input type="button" onclick="showTableData()" class="btn btn-danger float-right" value="Leer" />
            </a>
        </div>
    
        <div class="row">
            <button type="submit" class="btn btn-primary float-right">Guardar</button>
        </div>
    
        </form>

    The idea is that a number of managers can be added, but I cannot find a way to add it that when returning to the controller brings the information of Company Responsible .

    Basically the idea is to pass an array from the view to the controller, but I have the problem handling the ICollection

    Thanks a lot!

    Saturday, May 2, 2020 4:29 PM

Answers

  • User-854763662 posted

    Hi nestat92 ,

    Change your  function myFunction()  as shown:

    function myFunction() {
            var table = document.getElementById("myTable");
            var i = $("#myTable tbody tr").length-1;
            table.contentEditable = true;
            var row = table.insertRow(2);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            var cell3 = row.insertCell(2);
            var cell4 = row.insertCell(3);
            var cell5 = row.insertCell(4);
            var cell6 = row.insertCell(5);
            cell1.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Post'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Post' data-valmsg-replace='true'></span>";
            cell2.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Name'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Name' data-valmsg-replace='true'></span>";
            cell3.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].MaternalName'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_MaternalName' data-valmsg-replace='true'></span>";
            cell4.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].ParentName'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_ParentName' data-valmsg-replace='true'></span>";
            cell5.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Mail'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Mail' data-valmsg-replace='true'></span>";
            cell6.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Telephone'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Telephone' data-valmsg-replace='true'></span>";
      }

    Result

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2020 9:58 AM

All replies

  • User-474980206 posted

    table values don't post back. only inputs, selects or textarea. where you planning on ajax for posting or a form post?

    also if your model has an ICollection property, the model needs to initialize it in its constructor, as the binder does not know create one as it does not know the datatype.

     

    Saturday, May 2, 2020 9:16 PM
  • User419418919 posted

    Hi Bruce! Thanks for response me, look, I think that if the constructor is missing in the "Responsible Company" model. I attach some screenshots of the program.

    The site has an index screen, where I can view the general data of the company, from there you can modify, view details, delete and add a new company. Indeed the form you mention has many entries but I am using them to return it to the controller.

    When Im debuggin have the next screen:

    I think it would help if you can give me an example of how I can enter data into the collection Responsible Company from the controller

    Thank you!

    Saturday, May 2, 2020 11:56 PM
  • User-854763662 posted

    Hi nestat92 ,

    Change your  function myFunction()  as shown:

    function myFunction() {
            var table = document.getElementById("myTable");
            var i = $("#myTable tbody tr").length-1;
            table.contentEditable = true;
            var row = table.insertRow(2);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            var cell3 = row.insertCell(2);
            var cell4 = row.insertCell(3);
            var cell5 = row.insertCell(4);
            var cell6 = row.insertCell(5);
            cell1.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Post'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Post' data-valmsg-replace='true'></span>";
            cell2.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Name'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Name' data-valmsg-replace='true'></span>";
            cell3.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].MaternalName'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_MaternalName' data-valmsg-replace='true'></span>";
            cell4.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].ParentName'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_ParentName' data-valmsg-replace='true'></span>";
            cell5.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Mail'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Mail' data-valmsg-replace='true'></span>";
            cell6.innerHTML = "<input style='border: 0'  name='ResponsibleCompanies[" + i + "].Telephone'/>" + "<span class='text-danger field-validation-valid' data-valmsg-for='ResponsibleCompanies[0]_Telephone' data-valmsg-replace='true'></span>";
      }

    Result

    Best Regards,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2020 9:58 AM
  • User419418919 posted

    Hi Sherry thanks for you response, it helped a lot, it was exactly what I needed.

    I am learned to program in ASP Core what books or videos do you recommend.

    Thanks a Lot!

    Monday, May 4, 2020 4:25 PM
  • User-854763662 posted

    Hi nestat92 ,

    Microsoft's official documentation covers ASP.NET Core well, you can learn asp.net core step by step according to the left navigation bar.

    https://docs.microsoft.com/en-us/aspnet/core/introduction-to-aspnet-core?view=aspnetcore-3.1

    Best Regards,

    Sherry

    Wednesday, May 6, 2020 1:27 AM