IEnumerable ViewModel 不允许我访问模型属性

发布于 2025-01-07 08:01:21 字数 878 浏览 4 评论 0原文

当它是 IEnumerable 类型时,我在访问视图内的 Model 属性时遇到问题。

我的控制器代码:

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

在我看来:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage< IEnumerable>" %>

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

I'm having trouble accessing the properties of Model inside my view, when it's an IEnumerable type.

My controller code:

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

And in my view:

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

I'm sure I've done this before, but I'm not sure if I'm missing something obvious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2025-01-14 08:01:21

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

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

或者完全限定对 IEnumerable 的引用:
System.Web.Mvc.ViewPage>

You need to include a using to get the System.Collections.Generic namespace in your page:

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

Or fully qualify the reference to IEnumerable<T>:
System.Web.Mvc.ViewPage>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文