2 models in a view razor asp.net mvc

0

Good morning I have the following problem create a model to put in another two models to show their data in a single view if I put

 public List<Producto> Productos {get; private set;}
 public DetalleEntrega Detalle {get; private set;}

At the time of putting: @Html.DisplayNameFor(model=>.Producto.campo) tells me which field is not in the context and if you already check which field is in my table products

If I change it to the list, I do not get that error but I do it in the

@foreach(var item in Model.Productos) I get an error that is not of type GetEnumerator.

 public Productos {get; private set;}
 public DetalleEntrega Detalle {get; private set;}

The two models within one

public class Mimodelo
{
   public List<Producto> Productos { get; set; }
   public DetalleEntrega Detalle { get; set; }
}

The action resulted in my controller Products

public ActionResult Entrega(int id) 
{ 
var Productos = db.Productos.Where(d => e.IdEntrega==id).Include(p => p.Compra).Include(p => p.Empresa); 

var miModelo = new MiModelo();
miModelo.Productos = Productos.ToList();
miModelo.DetallesEntrega = new DetallesEntrega();

return View(miModelo); 
} 

View

@model Empresa.Models.Mimodelo

@{
    ViewBag.Title = "Entrega";
}

<h2>Entrega</h2>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Productos.ProductName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Entrega.Purpose)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Productos.Material)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Entrega.Embarque)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Entrega.Compras)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Productos.Comments)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Entrega.EmpresaT.Empresa)
        </th>

        <th></th>
    </tr>

@foreach (var item in Model.Productos) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Productos.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Entrega.Purpose)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Productos.Material)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Entrega.Embarque)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Entrega.Compras)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Productos.Comments)
        </td>

        <td>
            @Html.DisplayFor(modelItem => item.Entrega.EmpresaT.Empresa)
        </td>

        <td>


        </td>
    </tr>
}

</table>
    
asked by senseilex 31.08.2017 в 16:31
source

3 answers

1

campo does not exist because campo is not a property of List<Producto> but of Producto

By the form of your model it means that @Model has a list of products that you will need to iterate with a foreach for example to access each item in the list. It is the Producto and not the List<Producto> the one that has the campo

Example. For this model:

public class Model
{
   public List<Producto> Productos { get; private set; }
   public DetalleEntrega Detalle { get; private set; }
}

This would be in view:

@foreach(var producto in Model.Productos)
{
    // Acá si debería existir producto.campo
}
    
answered by 31.08.2017 / 16:44
source
0

The campo property will not appear because the Productos property is type List<T> and the list types do not have a property called campo .

You will have to include in the model a property called campo so you can do what you want:

public class ViewModel
{
  public List<Producto> Productos {get; private set;}
  public DetalleEntrega Detalle {get; private set;}
  public string campo{ get; set;}
}

Then in the view:

@Html.DisplayNameFor(model=> model.campo)
    
answered by 31.08.2017 в 16:41
-1

Regarding the error that appears when it tells you that it is not a GetEnumerator, it is because in the cshtml where you are implementing the displayName, at the beginning of the document you have to go @model IEnumerator<Empresa.Models.Mimodelo> and not @model Empresa.Models.Mimodelo

    
answered by 03.04.2018 в 16:56