Multi Models in an ASP NET MVC view

0

Hi, I'm new to C # and MVC ASP NET and I have the following complication. I'm trying to flatten the data of different models in a single view to use this data in a form, to all this I found that I can (mix?) Two models or more in a parent model and pull this new data from both models. ..
If this is the case, I have already tried but I can not do it.
These are my models:

using System.ComponentModel.DataAnnotations;

namespace PCotiza_compras.Models
{
    public class Categories
    {
        public int Id { get; set; }

        [Display(Name = "Nombre")]
        public string Name { get; set; }

        [Display(Name = "Descripción")]
        public string Description { get; set; }

    }

}

using System.ComponentModel.DataAnnotations;

namespace PCotiza_compras.Models
{
    public class Departments
    {
        public int Id { get; set; }

        [Display(Name = "Código")]
        public string Code { get; set; }

        [Display(Name = "Descripción")]
        public string Description { get; set; }
    }
}

In this other model I am trying to put the 2 together and I am also given other properties (I do not know if this is possible)

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace PCotiza_compras.Models
{
    public class Requests
    {
        List<PCotiza_compras.Models.Departments>  dep { get; set; }
        List<PCotiza_compras.Models.Categories> cat { get; set; }
    
        public int Id { get; set; }
        public string Folio { get; set; }
        public int UserId { get; set; }

        [Display(Name = "Proyecto")]
        public string Project { get; set; }

        [Display(Name = "Esquema")]
        public int SchemeId { get; set; }

        [Display(Name = "Fecha")]
        public string Date { get; set; }

        [Display(Name = "Estado")]
        public string Status { get; set; }

        [Display(Name = "Días de expiración")]
        public int ExpirationDays { get; set; }

        [Display(Name = "Comentarios")]
        public string Comments { get; set; }

        [Display(Name = "Departamento")]
        public int DepartmentId { get; set; }

        [Display(Name = "Categoría")]
        public int CategoryId { get; set; }
    }
}

My error I left here on the Model saying that I lack of definition 'GetNumerator'

@model PCotiza_compras.Models.Requests

@{
    ViewBag.Title = "Nueva solicitud";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<select class="form-control" id="sel1">
                                   
  @{ int i = 1; }
  @foreach (var item in Model.DepartmentId){
 <option>

<strong>@Html.DisplayFor(modelItem => item.Code)</strong>
  </option>
 i = i + 1;
 }

 </select>

Thanks a lot to the whole community:)

    
asked by E.Rawrdríguez.Ophanim 11.09.2017 в 23:16
source

1 answer

1

The error is on the next line

@foreach (var item in Model.DepartmentId)

The foreach statement expects that the list to be traversed is an object of a class that implements IEnumerable . In your case, the DepartmentId property is type int .

According to the class model Requests , I think it should look like this:

@foreach (var item in Model.dep)

Since dep is a list (which implements IEnumerable )

    
answered by 12.09.2017 / 23:26
source