【发布时间】:2016-06-03 06:19:39
【问题描述】:
这是我的看法。
<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 数据库中的数据填充表。我的控制器看起来像这样
public ActionResult Index()
{
OnlineModel model = new OnlineModel();
var result = model.GetAdminData();
return View();
}
在模型中我不知道如何使用 LINQ to SQL 查询
public string GetAdminData()
{
var result = ((from eng in odsEntities.EventLogs select eng.EventLogID).Distinct().ToList());
return "";
}
但我不知道如何完成剩下的步骤。用model写的代码是否正确?
【问题讨论】:
-
您的视图是否使用模型强类型化?
-
不,它不是强类型@Katana
-
我为您查找了一篇文章asp.net/mvc/overview/older-versions-1/models-data/… 花点时间阅读。为了降低复杂性,使您的视图具有强类型,以便您可以访问视图中的模型。
-
谢谢@Katana 我需要一些关于 LINQ to SQL 的说明
-
首先您的查询是创建
List<int>(假设EventLogID是int的类型,因此该方法需要是public List<int> GetAdminData()(不是string)及其return result;然后控制器方法需要向视图返回一些东西。但是你已经创建了一个OnlineModel然后把它扔掉了,你已经创建了一个List<int>并且把它也扔掉了所以不清楚你想要返回什么到视图。
标签: c# sql asp.net-mvc linq-to-sql