Show in details a list of ALL the materials that belong to two tables that have a one to many relationship with MVC 5 and EntityFramework

1

Greetings to all I am new to MVC and I have a problem to show the following model, the table OutMaterial stores the permissions on some kind of material that goes out, the table OutlineMaterial stores specifically the materials that are going to leave with respect to the associated permission of the table OutputMaterial .

I did a model class where I add the two tables

public class SalidaMaterial_LineaSalidaMaterial{
    //obtiene las tablas automaticamenete
    public SalidaMaterial salidaMaterial { get; set; }
    public LineaSalidaMaterial lineaSalidaMaterial { get; set; }
}

In the Details.cshtml section, I'll call that model to show it in the view

@model _VigilanciaIMO.Models.SalidaMaterial_LineaSalidaMaterial

What I have a problem with is the following, I found an example on which I relied but this example only gets ONE SINGLE record and what interests me is to show ALL that belong to the table OutputMaterial .

For that I have the following class SalMat_LinSalMat_Show.cs with the following method

public class SalMat_LinSalMat_Mostrar {

    private VigilanciaEntities db = new VigilanciaEntities();

    public SalidaMaterial mostrarInformacion(int id){  
        try{
            var lista = 
                (
                    from s in db.SALIDAMATERIAL
                    join l in db.LINEASALIDAMATERIAL
                    on s.IdSalidaMaterial equals l.IdSalidaMaterialF
                    where s.IdSalidaMaterial == id
                    select s
                ).ToList();
            return lista.First();
           /* NECESITO MOSTRAR TODOS LOS REGISTROS PERTENECIENTES*/
        } catch (Exception e){
            Console.WriteLine(e.StackTrace);
            return null;
        }
    }
}

And in the controller in the Details part I do the following but to show only one record.

    // GET: AutorizarSalidaMaterial/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
       SalidaMaterial_LineaSalidaMaterial modelo = new SalidaMaterial_LineaSalidaMaterial();
        //el valor 19 es un valor fijo para pruebas
        modelo.salidaMaterial = new SalMat_LinSalMat_Mostrar().mostrarInformacion(19);
        modelo.lineaSalidaMaterial = modelo.salidaMaterial.LINEASALIDAMATERIAL.First();
        //hacer que muestre mas de uno 
        //buscar el contrario de FirstOrDefault()
        return View(modelo);
    }

Could you help me please to resolve this detail?

    
asked by Galaviz Arroyo 03.07.2018 в 19:49
source

1 answer

1

Surely you have to make several more touches. But to begin with, you should arrange the following things:

public List<SalidaMaterial> mostrarInformacion(int id){  
    try{
        var lista = 
            (
                from s in db.SALIDAMATERIAL
                join l in db.LINEASALIDAMATERIAL
                on s.IdSalidaMaterial equals l.IdSalidaMaterialF
                where s.IdSalidaMaterial == id
                select s
            ).ToList();
        return lista;
    } catch (Exception e){
        Console.WriteLine(e.StackTrace);
        return null;
    }
}

As your objects are armed, a List may not reach you and you need another type of object. Make sure you take out the First, which was the one that made you return the first. Maybe instead of list you need Array or ImmutableArray.

Your controller should look something like this:

public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
   SalidaMaterial_LineaSalidaMaterial modelo = new SalidaMaterial_LineaSalidaMaterial();
    //el valor 19 es un valor fijo para pruebas
    modelo.salidaMaterial = new SalMat_LinSalMat_Mostrar().mostrarInformacion(19); <-- Hay que cambiar el objeto, para que acepte una lista, array....
    modelo.lineaSalidaMaterial = modelo.salidaMaterial.LINEASALIDAMATERIAL.First(); <-- first siempre devuelve el primero, sacalo y devuelve todo lo que queres. Tal vez tengas que usar, ToList()?
    //hacer que muestre mas de uno 
    //buscar el contrario de FirstOrDefault()
    return View(modelo);
}

Your object should also change:

public class SalidaMaterial_LineaSalidaMaterial{
    //obtiene las tablas automaticamenete
    public List<SalidaMaterial> salidaMaterial { get; set; }
    public List<LineaSalidaMaterial> lineaSalidaMaterial { get; set; }
}
    
answered by 03.07.2018 в 20:46