我正在尝试使用剃刀视图引擎开发Web应用程序。这是一个休假请求管理系统,用户登录网站并通过网络表单提交休假请求。所有请求都存储在名为" LeaveRequests"的数据库表中。目前我正在尝试更改页面,因此当用户登录时,他们所做的所有休假请求都会在表格视图中显示在网页上。下面显示的代码可以正常显示用户发出的1个请求,但我需要更改它以显示用户发出的所有请求。我尝试过使用foreach语句,但是无论我尝试什么都不断出错,有人能指出我正确的方向并告诉我如何改变我的代码以实现我想要的东西吗?
var db = Database.Open("Annual Leave System");
var dbCommand2 = "SELECT * FROM LeaveRequests WHERE email = @0";
var row2 = db.QuerySingle(dbCommand2, theEmail);
if(row2 != null) {
description = row2.description;
theLeaveType = row2.leaveType;
startDate = row2.startDate;
endDate = row2.endDate;
shortStartDate = startDate.ToString("dd-MMMM-yyyy");
shortEndDate = endDate.ToString("dd-MMMM-yyyy");
inttotalDays = row2.totalDays;
requestStatus = row2.requestStatus;
}
<fieldset>
<legend>Employee Leave Request Details</legend>
<table border="1" width="100%">
<tr bgcolor="grey">
<th>Description</th>
<th>Leave Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total days leave requested</th>
<th>Request Status</th>
</tr>
<tr>
<th>@description</th>
<th>@theLeaveType</th>
<th>@shortStartDate</th>
<th>@shortEndDate</th>
<th>@inttotalDays</th>
<th>@requestStatus</th>
</tr>
</table>
</fieldset>
答案 0 :(得分:1)
QuerySingle方法只返回一行。您需要使用Query方法获取所有行。
var rows = db.Query(dbCommand2, theEmail);
然后在文件的HTML部分:
<fieldset>
<legend>Employee Leave Request Details</legend>
<table border="1" width="100%">
<tr bgcolor="grey">
<th>Description</th>
<th>Leave Type</th>
<th>Start Date</th>
<th>End Date</th>
<th>Total days leave requested</th>
<th>Request Status</th>
</tr>
@foreach(var row in rows){
<tr>
<td>@row.description</td>
<td>@row.leaveType</td>
<td>@row.startDate.ToString("dd-MMMM-yyyy")</td>
<td>@row.endDate.ToString("dd-MMMM-yyyy")</td>
<td>@row.totalDays</td>
<td>@row.requestStatus;</td>
</tr>
}
</table>
此处提供更多信息:
http://www.mikesdotnetting.com/Article/214/How-To-Check-If-A-Query-Returns-Data-In-ASP.NET-Web-Pages http://www.asp.net/web-pages/tutorials/data/5-working-with-data