C# 如何在mvc视图中显示表中的数据库数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16168648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:13:59  来源:igfitidea点击:

how to display database data in tables in view in mvc

c#asp.net-mvc

提问by pihu

In my MVC application am retrieving data from Database. I want to display the retired data in tables. controller code:

在我的 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();
    }

view code:

查看代码:

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

This code displays the data But i want to display it in tabular format.

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

How can i display the data in tabular format?

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

回答by mattytommo

Don't use ViewData, use a Model built using a custom class to store that information. Something like:

不要使用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; }
}

Then you can do:

然后你可以这样做:

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);

Then in your view you can do:

然后在您看来,您可以执行以下操作:

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

回答by Ali Reza

Add follow code Before Html Code in your Model

在模型中的 Html 代码之前添加跟随代码

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