mvc 显示mysql数据库数据_如何在MVC视图中的表中显示数据库数据

bd96500e110b49cbb3cd949968f18be7.png

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

controller code:

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:

Info

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

How can i display the data in tabular format?

解决方案

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

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

if (Model.Count > 0)

{

File NameDateTimeInfo

@foreach (TableInfo item in Model)

{

@item.FileName@item.Date@item.Time@item.Info

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值