【问题标题】:IEnumerable ViewModel doesn't allow me to access Model propertiesIEnumerable ViewModel 不允许我访问模型属性
【发布时间】:2012-03-10 22:51:15
【问题描述】:

Model 是 IEnumerable 类型时,我无法访问视图内的属性。

我的控制器代码:

//
// GET: /Home/
public ActionResult Index()
{
    List<CourseworkQuestion> courseworkQuestions = repository.GetCourseworkQuestions(repository.GetUsername()).ToList();
    List<HomeViewModel> model = new List<HomeViewModel>();

    foreach (CourseworkQuestion courseworkQuestion in courseworkQuestions)
    {
        HomeViewModel hvm = new HomeViewModel(courseworkQuestion);
        model.Add(hvm);
    }

    return View(model);
}

在我看来:

&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;IEnumerable&lt;InternalAssessmentViewer.ViewModels.HomeViewModel&gt;&gt;" %&gt;

我确定我以前做过,但我不确定我是否遗漏了一些明显的东西。

【问题讨论】:

    标签: c# asp.net-mvc viewmodel ienumerable


    【解决方案1】:

    您需要包含 using 以在您的页面中获取 System.Collections.Generic 命名空间:

    <%@ Import namespace="System.Collections.Generic" %>
    <%@ Import namespace="System.Linq" %> //To get all the LINQ extension methods
    

    或完全限定对IEnumerable&lt;T&gt; 的引用: System.Web.Mvc.ViewPage>

    【讨论】:

    • 谢谢。我是 ASP.NET 和 MVC 的新手。你能告诉我如何做包含吗?
    • 我似乎获得了其他方法和扩展(例如 Aggregate、All 等),但没有我的属性。
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    相关资源
    最近更新 更多