views:

445

answers:

2

Made solution change: I am trying to display a html table of data.. In my controller I start an object as null and then pass the object as a reference to update the object based on the info in the DB like so "user control named(Indexcontrol.ascx)":

        List<dataob> data = null;
        dataManager target = new dataManager();
        //pass the parameter to a stored procedure and update it
        target.LoadFromDatabase(ref data);
        this.ViewData.Model =data;
        return View("Index");

I am trying to see how to display a table once the information is in the data object using a similar route all this is in the user control

 <tbody >

<% foreach (businesslayer.dataob m in  ViewData.Model) 
{ %>
<tr>
 <td><%= m.ID%></td>
 <td><%= m.Date %></td>
 <td><%= m.Description %></td>
 </tr>
 <% } %>


</tbody>

I figured out the problem....since I had the table attribute set to runat=server thats what gave me the error..don't know why but it did

A: 

Try:

<% foreach (dataob m in (IEnumerable<dataob>) ViewData["data"]) { %>
Garry Shutler
that <dataob> shows a syntax error saying that type or namespace name 'dataob' can not be found. but I imported the same class that I imported in the controller?
TStamper
You may need to import the namespace of dataobj in your web.config? I can only guess without seeing the whole project.
Garry Shutler
use <%@ Import Namespace="your-dataob-namespace" %> in the view
tvanfosson
Note: I don't think this is required if you use a strongly-typed view as I suggest in my answer.
tvanfosson
+5  A: 

I'm not sure why you are avoiding the ViewData.Model. There is no reason, that I can see in this case, why:

 ViewData["data"] = data;

is preferrable to

 ViewData.Model = data;

If you used a strongly typed View page, you could then avoid the need to cast the Model as well. Then you could simply do:

 <% foreach (dataob m in ViewData.Model) { %>
    <tr> 
        <td><%= m.Id %></td>
        <td><%= m.user %></td>
        <td><%= m.Date %></td>
    </tr>
 <% } %>
tvanfosson
This is how I'd do it myself, without the seemingly irrational fear of using ViewData.Model
Garry Shutler
The "fear" comes from the last sentence of his question. He needs to use a strongly typed model.
Craig Stuntz
I did that and it works but now I am trying to integrate it within a user control and it will not work
TStamper
Is your user control strongly typed as well?
tvanfosson
yes it is strongly typed..you mean this right in code behind? public partial class Datacontrol : System.Web.Mvc.ViewUserControl<List<dataob>>
TStamper
But you aren't passing the list to the user control, only one item from the list. You want it to be ViewUserControl<dataob>, I think.
tvanfosson