IEnumerable ViewModel 不允许我访问模型属性
当它是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在页面中包含一个 using 来获取
System.Collections.Generic
命名空间:或者完全限定对
IEnumerable
的引用:System.Web.Mvc.ViewPage>
You need to include a using to get the
System.Collections.Generic
namespace in your page:Or fully qualify the reference to
IEnumerable<T>
:System.Web.Mvc.ViewPage>