迭代列表中的错误在C#中 [英] Error during iterates list iin C#

查看:80
本文介绍了迭代列表中的错误在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以前加载值的员工的列表。我尝试在foreach的视图中迭代它们,但它没有办法。编译时出错:

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>

推荐答案

您将返回单个Alumno实体查看

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



并在视图中使用列表。



首先清除你的要求。你是否真的想在视图上显示列表,如果没有那么不要在视图中使用列表。



两个选项



1.从视图中删除列表



2.从控制器传递校友名单


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);


这篇关于迭代列表中的错误在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆