英文:
how can I show the date in a columns header every day with total?
问题
To display the date in a column and show each day's data horizontally in your MVC view, you can pivot the data in your controller and modify the view accordingly. Here's how you can achieve this:
Controller:
public ActionResult SamplesSttat()
{
int HospitalId = Convert.ToInt32(Session["HospitalID"]);
var sTATISTICS_DAILY = db.STATISTICS_DAILY.Include(s => s.stat_Departments)
.Include(s => s.Hospital).Where(x => x.hospital_no == HospitalId)
.Include(s => s.sys_users1).OrderByDescending(stat => stat.DATE).ToList();
// Pivot the data
var pivotData = sTATISTICS_DAILY.GroupBy(stat => stat.stat_Departments.Dept_name)
.Select(group => new
{
Department = group.Key,
Data = group.Select(stat => new
{
Date = stat.DATE.ToString("dd/MM/yyyy"),
MorningSamples = stat.SAMPLES_MORNING,
EveningSamples = stat.SAMPLES_EVENING
})
}).ToList();
ViewBag.PivotData = pivotData;
return View();
}
View:
@model IEnumerable<warehouse.Models.STATISTICS_DAILY>
@{
ViewBag.Title = "SamplesSttat";
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
<h2>Statistic</h2>
<table class="table">
<tr>
<th>
Department
</th>
@foreach (var item in ViewBag.PivotData.First().Data)
{
<th>
@item.Date
</th>
}
</tr>
@foreach (var pivotItem in ViewBag.PivotData)
{
<tr>
<td>
@pivotItem.Department
</td>
@foreach (var dataItem in pivotItem.Data)
{
<td>
@dataItem.MorningSamples || @dataItem.EveningSamples
</td>
}
</tr>
}
</table>
This code will pivot the data so that each date becomes a column, and the view will display the data as you described, with the date in a column and each day's data shown horizontally.
英文:
I need to make the statistic page in MVC how to make the controller and view to be like this output and show the date in the column with every day for example :
department day1||(SAMPLES_MORNING) , day1 || (SAMPLES_EVENING) , day2 || (SAMPLES_MORNING) , day2 || (SAMPLES_EVENING) , .......
this is the model class :
public partial class STATISTICS_DAILY
{
public int ID { get; set; }
public Nullable<int> DEPT_ID { get; set; }
public Nullable<int> SAMPLES_MORNING { get; set; }
public Nullable<int> SAMPLES_EVENING { get; set; }
public Nullable<System.DateTime> DATE { get; set; }
public Nullable<int> hospital_no { get; set; }
public Nullable<int> user_id { get; set; }
public virtual stat_Departments stat_Departments { get; set; }
public virtual sys_users sys_users { get; set; }
public virtual Hospital Hospital { get; set; }
public virtual sys_users sys_users1 { get; set; }
}
}
in the statistic 1M means first date morning total number of samples and 1E means first date evening total number of samples
2M means second date morning total number of samples and 2E means second date evening total number of samples and so on till last day
I tried this is the controller :
public ActionResult SamplesSttat()
{
int HospitalId = Convert.ToInt32(Session["HospitalID"]);
var sTATISTICS_DAILY = db.STATISTICS_DAILY.Include(s => s.stat_Departments)
.Include(s => s.Hospital).Where(x => x.hospital_no == HospitalId)
.Include(s => s.sys_users1);
return View(sTATISTICS_DAILY.OrderByDescending(stat => stat.DATE).ToList());
}
and this is the view :
@model IEnumerable<warehouse.Models.STATISTICS_DAILY>
@{
ViewBag.Title = "SamplesSttat";
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
<h2>Statistic</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.stat_Departments.Dept_name)
</th>
<th>
@Html.DisplayNameFor(model => model.SAMPLES_MORNING)
</th>
<th>
@Html.DisplayNameFor(model => model.SAMPLES_EVENING)
</th>
<th>
@Html.DisplayNameFor(model => model.DATE)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.stat_Departments.Dept_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.SAMPLES_MORNING)
</td>
<td>
@Html.DisplayFor(modelItem => item.SAMPLES_EVENING)
</td>
<td>
@Html.DisplayFor(modelItem => item.DATE)
</td>
</tr>
}
</table>
This is the current output view :
How can I put the date column up and view each day in a column not in the rows like the statistic
答案1
得分: 0
我用不同的方法解决了它
我做了一个水晶报表的交叉表报告,这种报告可以进行统计
此外,如果我找到方法,我将尝试在MVC控制器和视图中完成它,我会发布它
谢谢
英文:
I solved it in different way
I did a crystal report cross-tab report this kind of report will do such statistics
Also I will try to do it in MVC controller and view if I find the way I will post it
Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论