控制器动作:不是强类型的视图问题
public ActionResult Index()
{
probaEntities proba = new probaEntities();
probaEntities proba1 = new probaEntities();
probaEntities proba2 = new probaEntities();
var query = from i in proba.name
from j in proba1.id_person
from k in proba2.last_name
select new
{
i.ime,
j.id,
k.prezime
};
return View(query);
}
查看网页:
<table>
<tr>
<th>
ime
</th>
<th>
broj
</th>
<th>
prezime
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.name) %>
</td>
<td>
<%= Html.Encode(item.id_person)%>
</td>
<td>
<%= Html.Encode(item.last_name)%>
</td>
</tr>
<% } %>
</table>
如何才能在继承=写 “???” ?这个视图不是强类型的,因为我有3个表格向他发布数据。我是否必须为这个观点制定一个特殊的模型,是否有一个较短的解决方案?
在这种情况下,我有这个错误:foreach语句不能对'object'类型的变量进行操作,因为'object'不包含'GetEnumerator'的公共定义 – Ognjen 2010-04-26 18:17:21
如果你想枚举,你可以创建自己的类代表您的数据模型并实现IEnumerable。然后你会使用'Inherits =“System.Web.Mvc.ViewPage”',将它从控制器传递到视图中。 –
RedFilter
2010-04-26 18:20:43
当我编写<%@ Page Title =“”Language =“C#”Inherits =“System.Web.Mvc.ViewPage”%>时,我可以这样做,而不需要我自己的班级代表数据模型 – Ognjen 2010-04-26 18:23:40