在MVC 3.0中的ASPX页面中显示数据表 [英] Show datatable in ASPX page In MVC 3.0
问题描述
Hi Friends
我已经在控制器中动态创建了数据表.
我想在视图上显示它.
我已经在google上搜索过,但是字段与模型相关联,但是我的datatabel与放置在页面上的该模型无关(继承).
代码示例
ViewData ["showdata"] = datatable;
我想在aspx页面上显示ViewData ["showdata"].
控制器代码=============
Hi Friends
I have created the datatable dynamically in the controller.
I want to show it on the view.
I have searched on google but the fields are associated with the model but my datatabel is not associated with that model placed on the page(Inherited).
code sample
ViewData["showdata"]=datatable;
I want to show the ViewData["showdata"] on the aspx page.
Controller code=============
for (int i = 1; i <15; i++)
{
dr= dt.NewRow();
string yearList = Convert.ToString("Year " + i);
dr["Years"] = yearList.ToString();
if (Session["OldProRev"] !=null)
dr["Projectrevenues"] = (1 + CagrRate/100) * Convert.ToDecimal(Session["OldProRev"]);
else
dr["Projectrevenues"] = (1 + CagrRate / 100) * amount;
Session["OldProRev"] = dr["Project revenues"];
dt.Rows.Add(dr);
}
ViewData["DisplayReport"] = dt;
}
return View(ViewData["DisplayReport"]);
=======================
查看代码
========================
view code
<% foreach (var result in (IEnumerable<dynamic>)ViewData["DisplayReport"])
{ %>
<tr>
<td>
<%=result.Projectrevenues%>
</td>
<td>
<%= result.Years%>
</td>
</tr>
<% } %>
请帮帮我.
谢谢
Please help me.
Thanks
推荐答案
您不应该使用这种viewdata.创建一个视图模型,使您的页面具有较强的类型,然后将代码以获取对象,并放入视图模型中.另外,使用Razor,而不是旧的语法,并且您没有aspx,这是MVC,它是cshtml.
You should not be playing with viewdata like this. Create a viewmodel, make your page strongly typed, and put the code to get the objects, in your viewmodel. Also, use Razor, not the old syntax, and, you have no aspx, this is MVC, it''s a cshtml.
这篇关于在MVC 3.0中的ASPX页面中显示数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!