Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a List with employees with previously loaded values. I try to iterates them in a view with a foreach but there is no way it works. I have an error when compile:
The model element passed to the dictionary is of type 
'Humano2.Models.Alumno', but this dictionary requires a model element of type 'System.Collections.Generic.List1 [Humano2.Models.Alumno]'
.

What I have tried:

Class persona
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Humano2.Models
{
    public class Persona
    {
        public int Dni;
        public string Nombre;
        public Persona()
        {


        }
     }   

}


Class alumno
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Humano2.Models
{


    public class Alumno : Persona
    {

        public int Edad;

        public Alumno(string nombre):base()
        {
            Edad = 18;
            if (lista.Count == 0)
            {
                this.altaAlumno(1111, "Julian", 20);
                this.altaAlumno(2222, "Pedro", 30);
                this.altaAlumno(3333, "Pablo", Edad);
            }
        }


        public Alumno(int edad):this()
        {
            this.Edad = edad;
        }
        public Alumno()
        {
            this.Nombre = "NN";
        }

        public static List<Alumno> lista = new List<Alumno>();

        public void altaAlumno(int Dni, string Nombre, int Edad)
        {
            lista.Add(new Alumno() { Dni = Dni, Nombre = Nombre, Edad = Edad });
        }
    }


controller
using Humano2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Humano2.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {


            Alumno p1 = new Alumno();
return View(p1);



        }
    }

}


View

@model List<Humano2.Models.Alumno>
    Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
             @foreach (Humano2.Models.Alumno c in Model)
        {
            <p>@c.Nombre</p>
           <p>@c.Dni</p>
           <p>@c.Edad</p>
        }

        </div>
    </body>
</html>
Posted
Updated 14-May-17 18:40pm
v2

1 solution

You are returning single Alumno entity to view
Alumno p1 = new Alumno();
return View(p1);

And using as list in view.

First clear your requirement. Are you really want to show list on view if not then dont use list in view.

Two option

1. Either remove list from view

2. Pass list of alumno from controller
List<Alumno> p1 = new List<Alumno>();
            p1.Add(new Alumno());
            return View(p1);
 
Share this answer
 
v3
Comments
Member 12817768 15-May-17 18:11pm    
don´t work, in the view shows NN 0 0
pradiprenushe 16-May-17 1:04am    
THis correct output. You are create object which uses parameter-less constructor. so Nombre value is set. Other have default values.

This constrcutor is called when you are creating object
public Alumno()
{
this.Nombre = "NN";
}
Member 12817768 16-May-17 12:47pm    
thank you
pradiprenushe 17-May-17 2:30am    
Welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900