我想格式化我的 linq 查询的结果并将其传递给网格。我有第一个日期(星期一的日期)作为我的输入,我用它来提取那周的数据(星期一到星期日。
这是我存储在 DB 中的内容,它是通过 linq 查询提取的:
Date Hours
07/29/2013 1.3
07/29/2013 2.0
07/30/2013 3.1
07/31/2013 0.1
07/31/2013 5.2
08/01/2013 1.1
08/01/2013 1.1
08/01/2013 2.2
08/02/2013 3.3
08/02/2013 4.0
08/03/2013 2.1
这是我想在网格中显示输出的方式:
Day Hours
Monday 2.3
Tuesday 3.1
Wednesday 5.3
Thursday 4.4
Friday 9.5
Saturday 2.1
Sunday 0.0
控制器动作:
[HttpPost]
[GridAction]
public ActionResult GridOutput(string dateOnMonday)
{
DateTime DateOnMonday = Convert.ToDateTime(dateOnMonday);
var WeeklyHoursToGrid = (from h in db.vwHours.Where(h.Date >= DateOnMonday && h.Date < DateOnMonday.AddDays(7))
select h.Hours).Sum();
return View(new GridModel { Data = WeeklyHoursToGrid });
}
HTML(网格):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<UI.Models.vwHour>>" %>
<div>
<% Html.GridFor("WeekDetails", "GridOutput", "Hours", GridOptions.EnableSelecting, Model).Columns(column =>
{
column.Bound(o => o.Date).Title("Day");//Display Day's instead of Date
column.Bound(o => o.Hours).Title("Hours");//Display Total Hours for that day
}).Render();
%>
</div>
任何帮助深表感谢