【问题标题】:how to display database data in tables in view in mvc如何在mvc的视图中显示表中的数据库数据
【发布时间】:2013-04-23 11:51:58
【问题描述】:

在我的 MVC 应用程序中,我正在从数据库中检索数据。我想在表格中显示退役数据。 控制器代码:

 public ActionResult MyAccount()
    {

        var user = User.Identity.Name;

       string sThumbnails = "";

        DataSet dsTemplates = new DataSet();

        string qryTemplets = "select * from FileInfo where UserName= '" + user + "'";

        open_Connection();
        SqlDataAdapter daTemplate = new SqlDataAdapter(qryTemplets, conn);
        daTemplate.Fill(dsTemplates, "FileInfo");
        DataTable dtTemplates = new DataTable();
        dtTemplates = dsTemplates.Tables[0];

        foreach (DataRow row in dtTemplates.Rows)
        {

            sThumbnails = sThumbnails +  row["FileName"] +  row["Date"] +  row["Time"] +  row["Info"] ;
            ViewData["thumbs"] = sThumbnails;
        }
        close_Connection();
        return View();
    }

查看代码:

    <div id="formleft" style="border-style: solid; border-color: inherit; border-width: 4px; width:550px; height:500px; position:absolute; top: 345px; left: 348px;">
   <h2 class="heading" style="text-align: center;">Info</h2><%= ViewData["thumbs"] %> </div>

此代码显示数据但我想以表格格式显示它。

如何以表格格式显示数据?

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    不要使用ViewData,而是使用使用自定义类构建的模型来存储该信息。比如:

    public class TableInfo
    {
        public string FileName { get; set; }
        public string Date { get; set; } //you might want to change this to DateTime
        public string Time { get; set; }  //this may need changing to TimeSpan or int possibly?
        public string Info { get; set; }
    }
    

    那么你可以这样做:

    List<TableInfo> tables = dtTemplates.Rows.AsEnumerable()
        .Select(t => new TableInfo
            {
                FileName = row["FileName"],
                Date = row["Date"],
                Time = row["Time"],
                Info = row["Info"]
            })
        .ToList();
    
        close_Connection();
        return View(tables);
    

    那么在你看来你可以这样做:

    @model List<TableInfo>
    
    if (Model.Count > 0)
    {
        <table>
            <tr>
                <td>File Name</td>
                <td>Date</td>
                <td>Time</td>
                <td>Info</td>
            </tr>
            @foreach (TableInfo item in Model)
            {
                <tr>
                    <td>@item.FileName</td>
                    <td>@item.Date</td>
                    <td>@item.Time</td>
                    <td>@item.Info</td>
                </tr>
            }
        </table>
    }
    

    【讨论】:

    • .Select(t => new FileInfo as: 'System.Data.DataRowCollection' 不包含'Select' 的定义,并且没有扩展方法'Select' 接受第一个参数可以找到类型“System.Data.DataRowCollection”(您是否缺少 using 指令或程序集引用?)
    • 还有一件事是来自数据库的 FileInfo 表包含所有用户的记录。我只想显示登录用户的数据。我在 MVC2 中开发这个应用程序
    • 啊,你需要使用Rows.AsEnumerable()。确保你也添加using System.Linq;
    • 你能给我完整的代码吗..我还是很困惑。我要它紧急。所以请
    • @pihu 检查我的更新编辑。你需要做Rows.AsEnumerable()
    【解决方案2】:

    在模型中的 Html 代码之前添加后续代码

    <style> 
       table th {  border: 1px solid black; text-align:center }
       table td {  border: 1px solid black; text-align:center } 
      </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2013-09-21
      • 2011-08-08
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      相关资源
      最近更新 更多