【发布时间】:2012-06-06 18:51:10
【问题描述】:
在 ASP.NET MVC3 Web 应用程序中。
我有意见。视图有一个 IEnumerable 模型。
我需要遍历模型的所有项目并显示 item.Name
观点:
@model IEnumerable<Object>
@{
ViewBag.Title = "Home";
}
@foreach (var item in Model)
{
<div class="itemName">@item.Name</div>
}
在控制器中,我使用 Linq to entity 从数据库中获取对象列表。
控制者:
public ActionResult Index()
{
IEnumerable<Object> AllPersons = GetAllPersons();
return View(AllSurveys);
}
public IEnumerable<Object> GetAllPersons()
{
var Context = new DataModel.PrototypeDBEntities();
var query = from p in Context.Persons
select new
{
id = p.PersonsId,
Name = p.Name,
CreatedDate = p.CreatedDate
};
return query.ToList();
}
运行时出现此错误:
'object' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
如何访问模型项的“名称”属性?
非常感谢您的帮助
【问题讨论】:
标签: c# asp.net-mvc-3 razor