Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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=============
C#
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

C#
<% foreach (var result in (IEnumerable<dynamic>)ViewData["DisplayReport"])
                      { %>
                   <tr>
                       <td>
                           <%=result.Projectrevenues%>
                       </td>
                       <td>
                           <%= result.Years%>
                       </td>
                                            </tr>
                   <% } %>


Please help me.

Thanks
Posted
Updated 6-Aug-12 8:03am
v5
Comments
Aditya Mangipudi 6-Aug-12 13:36pm    
Can you post your view and controller code and if possible the model which you built for this datatable.
Aditya Mangipudi 6-Aug-12 14:52pm    
Did you try creating a new class (MyClass) with members Projectrevenues and Years. Create a IEnumerable<myclass> in your model and pass it to view.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900