繁体   English   中英

如何使用ASP.net MVC从数据库中获取数据?

[英]How to fetch data from database using ASP.net MVC?


这是我的看法。

 <body> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid" style="font-size:13.9px;font-family:'Segoe UI';"> <div> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist" style="padding:0px;padding-top:10px"> <li role="presentation" class="activeIndex"><a href="#home" aria-controls="home" role="tab" data-toggle="tab" >EventLog</a></li> <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">ApplicationLog</a></li> </ul> <!-- Tab panes --> <div class="tab-content" style="padding-top:15px"> <div role="tabpanel" class="tab-pane active" id="home"> <table class="table table-striped "> <thead style="background-color: rgba(7,45,120,.8); color: white; font-size: initial;"> <tr> <th>EventLogID</th> <th>UserName</th> <th>Message</th> <th>Severity</th> <th>Type</th> <th>StackTrace</th> <th>PageSource</th> <th>CreatedDttm</th> </tr> </thead> </table> </div> <div role="tabpanel" class="tab-pane" id="profile"> <table class="table table-striped"> <thead style="background-color: rgba(7,45,120,.8); color: white; font-size: initial"> <tr> <th>EventLogID</th> <th>UserName</th> <th>UserValidationMessage</th> <th>DetailedMessage</th> <th>LogInTime</th> <th>LogOutTime</th> <th>CreatedDttm</th> </tr> </thead> </table> </div> </div><!-- /.container-fluid --> </nav> 

我通过创建表在mvc中创建了一个视图。我的目标是用sql数据库中的数据填充表。我的Controller看起来像这样

public ActionResult Index()
{
    OnlineModel model = new OnlineModel();
     var result = model.GetAdminData();
    return View();
}

在模型中,我不知道如何使用LINQ进行SQL查询

public string GetAdminData()
{
    var result = ((from eng in odsEntities.EventLogs select eng.EventLogID).Distinct().ToList());
    return "";
}

但我不知道如何执行其余步骤。 用模型编写的代码是否正确?

  1. 您必须将此数据发送到视图。 如下更改索引操作

     public ActionResult Index() { OnlineModel model = new OnlineModel(); var result = model.GetAdminData(); return View(result); } 
  2. 并修改了获取此数据的视图。

     @model IEnumerable<EventLog>// write it to head of page Perhaps you need to add true namespace for Eventlog class // then write it inside table <tbody> @foreach (item in Model) { <tr> <td>@item.EventLogID</td> <td>@item.UserName</td> <td>@item.Message</td> <td>@item.Severity</td> <td>@item.Type</td> <td>@item.StackTrace</td> <td>@item.PageSource</td> <td>@item.CreatedDttm</td> </tr> } </tbody> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM