在MVC中显示数据库数据表

时间:2021-10-14 13:08:47

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)":

做出解决方案更改:我正在尝试显示数据的html表..在我的控制器中,我将对象作为null启动,然后将对象作为参考传递,以根据数据库中的信息更新对象,如此“用户控件命名(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

我想出了问题....因为我将表属性设置为runat = server这就是什么给了我错误..不知道为什么但它确实

2 个解决方案

#1


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

我不确定你为什么要避开ViewData.Model。在这种情况下我没有理由看到原因:

 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>
 <% } %>

#2


Try:

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

#1


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

我不确定你为什么要避开ViewData.Model。在这种情况下我没有理由看到原因:

 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>
 <% } %>

#2


Try:

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