简体   繁体   English

“模型”不包含“对象”的定义

[英]'Model' does not contain a definition for 'Object"

I'm trying to print out data from an SQL query, but I'm running into error after error. 我正在尝试从SQL查询中打印出数据,但是一次又一次地遇到错误。

Bassically what Im trying to do is fetch some data from teachers out of a database, and then make a list of them on my view. 基本上,我想做的是从数据库中提取教师的一些数据,然后在我看来列出这些数据。 However everytime I try to print, I get this error 但是,每次尝试打印时,都会出现此错误

ViewModel' does not contain a definition for 'FirstName' and no extension method 'FirstName' accepting a first argument of type 'ViewModel' could be found. “ ViewModel”不包含“ FirstName”的定义,找不到包含“ ViewModel”类型的第一个参数的扩展方法“ FirstName”。

Here's my code; 这是我的代码;

ViewModel 视图模型

namespace Ability.Models
{
    public class ViewModel
    {
        public virtual IEnumerable<Teacher> Teachers { get; set; }
        public Teacher Teacher { get; set; }

        public virtual IEnumerable<Skill> Skills { get; set; }
        public Skill Skill { get; set; }
    }
}

Controller 控制者

public ActionResult Index()
        {
            string campus = Request.Params["Campus"];

            if (campus != null)
            {
                if (campus == "Alle")
                {
                    var teachers = (from t in db.Teachers
                                    select t);
                }
                else
                {
                    var teachers = (from t in db.Teachers
                                    where t.Campus == campus
                                    select t );
                }
            }
            else
            {
                var teachers = (from t in db.Teachers
                                select t);
            }

            var Model = new ViewModel
            {
                Teachers = db.Teachers.ToList(),
            };

            return View(Model);
        }

View 视图

@model IEnumerable<Ability.Models.ViewModel>

<h2>Teachers at Thomas More</h2>


               ...


@foreach (var Teacher in Model)
    {
        <tr>
            <td>
                @Teacher.FirstName
            </td>
            <td>
                @Teacher.LastName
            </td>
            <td>
                @Teacher.Email
            </td>
            <td>
                @Teacher.Campus
            </td>
        </tr>
    }

I'm not sure if it mathers, but I already tried doing several things including changing my loop to 我不确定它是否可以计算,但是我已经尝试做一些事情,包括将循环更改为

@foreach (var Teacher in Model.Teachers)

but then I receive a similar error 但是我收到类似的错误

'IEnumerable' does not contain a definition for 'Teachers' and no extension method 'Teachers' accepting a first argument of type 'IEnumerable' could be found “ IEnumerable”不包含“ Teachers”的定义,并且找不到找到接受“ IEnumerable”类型的第一个参数的扩展方法“ Teachers”

Looking forward to any help I can receive! 期待我能收到的任何帮助!

I see a couple of things that doesn't make sense. 我看到一些没有意义的事情。

First of all, where do you use the teachers ? 首先,您在哪里使用teachers

Personally, I think that the following code is more reasonable: 我个人认为以下代码更合理:

public ActionResult Index()
{

    IEnumerable<Teacher> teachers = Enumerable.Empty<Teacher>();

    string campus = Request.Params["Campus"];
    if (campus != null)
    {
        if (campus == "Alle")
        {
            teachers = (from t in db.Teachers
                        select t);
        }
        else
        {
            teachers = (from t in db.Teachers
                        where t.Campus == campus
                        select t );
        }
    }
    else
    {
        teachers = (from t in db.Teachers
                    select t);
    }

    var Model = new ViewModel
    {
        Teachers = teachers
    };

    return View(Model);
}

Second, you have to initialize also the other properties of ViewModel in the above action method. 其次,您还必须在上述操作方法中初始化ViewModel的其他属性。

Now let's go to the view. 现在,我们来看一下。 You pass there a model of type ViewModel . 您在那里传递了ViewModel类型的ViewModel When you want to iterate through the teachers sequence, you can try this: 当您要遍历教师顺序时,可以尝试以下操作:

@foreach (var teacher in Model.Teachers)
{
    <tr>
        <td>
            @teacher.FirstName
        </td>
        <td>
            @teacher.LastName
        </td>
        <td>
            @teacher.Email
        </td>
        <td>
            @teacher.Campus
        </td>
    </tr>
}

All the above makes sense provided that db.Teachers is an IEnumerable<Teacher> . 只要 db.TeachersIEnumerable<Teacher> ,上述所有内容都是有意义的。

You're sending the type ViewModel to the view: 您正在将ViewModel类型发送到视图:

var Model = new ViewModel
{
    Teachers = db.Teachers.ToList(),
};

return View(Model);

But it's expecting the type IEnumerable<Ability.Models.ViewModel> : 但是期望类型为IEnumerable<Ability.Models.ViewModel>

@model IEnumerable<Ability.Models.ViewModel>

That is, it's expecting a collection where you're providing an object . 也就是说,它期望在您提供对象集合中。

Given the usage in the view, it seems like you should just change it to expect a single object: 鉴于视图中的用法,似乎您应该对其进行更改以期望使用单个对象:

@model Ability.Models.ViewModel

This would allow you to directly access the property you're looking for: 这将允许您直接访问您要查找的属性:

@foreach (var Teacher in Model.Teachers)

Side Note: The logic in your controller doesn't look correct. 旁注:控制器中的逻辑看起来不正确。 You conditionally create a teachers variable, but then never use it. 您有条件地创建teachers变量,但是从不使用它。 Instead, you just materialize all of the teachers in the database and send them all to the view. 取而代之的是,您只需在数据库中实例化所有教师,然后将其全部发送到视图。 If you intend to see all of the teachers, you can remove most of that code. 如果您打算与所有老师见面,则可以删除大部分代码。 If you intend to see specific teachers, you would want to use the variable you created. 如果您打算看特定的教师,则需要使用您创建的变量。 Reference @Christos' answer for a practical example of that. 请参考@Christos的答案以获取实际示例。

Your model is a list of ViewModels. 您的模型是ViewModels的列表。 You needed to use @foreach (var Teacher in Model.Teachers) . 您需要使用@foreach (var Teacher in Model.Teachers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM