0

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

2 Answers 2

5

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>
 <% } %>
Sign up to request clarification or add additional context in comments.

6 Comments

This is how I'd do it myself, without the seemingly irrational fear of using ViewData.Model
The "fear" comes from the last sentence of his question. He needs to use a strongly typed model.
I did that and it works but now I am trying to integrate it within a user control and it will not work
Is your user control strongly typed as well?
yes it is strongly typed..you mean this right in code behind? public partial class Datacontrol : System.Web.Mvc.ViewUserControl<List<dataob>>
|
0

Try:

<% foreach (dataob m in (IEnumerable<dataob>) ViewData["data"]) { %>

4 Comments

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?
You may need to import the namespace of dataobj in your web.config? I can only guess without seeing the whole project.
use <%@ Import Namespace="your-dataob-namespace" %> in the view
Note: I don't think this is required if you use a strongly-typed view as I suggest in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.