Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导致foreach在我的mvc中出现问题_C#_Asp.net Mvc_Asp.net Mvc 4_Foreach - Fatal编程技术网

C# 导致foreach在我的mvc中出现问题

C# 导致foreach在我的mvc中出现问题,c#,asp.net-mvc,asp.net-mvc-4,foreach,C#,Asp.net Mvc,Asp.net Mvc 4,Foreach,正如你在这里看到的,这就是我之前发表的文章 我已经试着做了前面描述过的事情。但它会导致问题,因为我的foreach在运行时出错 Index.cshtml @foreach (var u in Model) { <div class="col-md-6 col-sm-6"> <div class="plan"> <h3>@u.Name<span>$@u.Price</span></h3&

正如你在这里看到的,这就是我之前发表的文章

我已经试着做了前面描述过的事情。但它会导致问题,因为我的foreach在运行时出错

Index.cshtml

@foreach (var u in Model)
 {
    <div class="col-md-6 col-sm-6">
       <div class="plan">
          <h3>@u.Name<span>$@u.Price</span></h3>
          <p>@u.Text</p>
       </div>
    </div>
 }
index.cshtml的顶部有:

@model MentorOrdblind_MVC.Models.Undervisning.Undervisning
型号低于ISNING.cs

// GET: Undervisning
    public ActionResult Index()
    {

        DatabaseClasseDataContext db = new DatabaseClasseDataContext();

        var model = db.Packages.ToList();

        return View(model);
    }
public class Undervisning
{

    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Hours { get; set; }
    public string Text { get; set; }

}

您正在向视图传递一个
列表
,但您的模型不是
IEnumerable的类型。因此,您的视图只需要一个类型为
undersining
的对象,而不是集合

使用以下命令:

@model IEnumerable<MentorOrdblind_MVC.Models.Undervisning.Undervisning>
@model IEnumerable

将您的模型增量更改为:

@model IEnumerable<MentorOrdblind_MVC.Models.Undervisning.Undervisning>
@model IEnumerable

此时,您的模型是单个类,而不是对象列表

请始终记住从控制器操作传递到视图的内容。如果仅从动作传递模型,则在动作的相应视图中使用模型引用。如果传递列表,则在视图中使用IEnumerable模型引用

If you pass list from action then in the view use:
@model IEnumerable<your model> in the top as reference
If you pass model without a list then use:
@model your model 
如果您通过操作传递列表,则在视图中使用:
@顶部的模型IEnumerable作为参考
如果传递模型时没有列表,则使用:
@模仿你的模型
在您的情况下,您正在传递列表,所以请使用所需模型类的IEnumerable


谢谢

@model.…欠平衡
建议使用单个对象,而不是集合,这样您就无法对其进行迭代。你有什么错误?请澄清这个问题。试着描述这个问题,你得到任何错误描述或异常吗?@StephenMuecke OP可以对它进行迭代,如果
欠压
实现
IEnumerable
请解释你所说的“它会导致问题,因为我的foreach在运行时出错”。你期望发生什么,实际发生了什么,以及你试图让这两个匹配的是什么?@CodeCaster:我记录了它是什么类型的数据类型,并添加了编辑。但是可能
低估了
类实现
IEnumerable
:-)这行变量模型=db.Packages.ToList();直接表示模型是列表现在它给了我一个错误:传入字典的模型项的类型为“System.Collections.Generic.List`1此错误清楚地说明了问题所在。您尚未共享有关您的类型结构的信息,因此我们不会猜出有什么问题。我记录了它是什么类型的数据类型,并添加了编辑。@Grundy我不懂。视图只需要一个对象,而不是集合。如果
Undervisning
中的属性是集合,那么我们应该查看模型来帮助回答这个问题。调用
Packages.ToList()
并直接将其分配给模型,意味着它是一个集合,而不是单个对象。如果类
欠压
实现
IEnumerable
,然后,一个对象是相等的集合,可以使用
foreach
@Grundy这行代码
var model=db.Packages.ToList()进行iterable
直接意味着模型现在是一个
列表
,因此如果
欠压
是一种
IEnumerable
类型,那么他现在有一个
列表
。这意味着他的视图仍然需要有
IEnumerable
声明。现在它给了我一个错误:传递到字典中的模型项是'System.Collections.Generic.List'类型`1@JesperPetersen好的,使用
@model List
,但这不重要。内部消息的完整错误是什么?