我在控制器中有以下内容:
outputmodel.Add(new SP_RESULTS.RS_Plans()
{
id = Convert.ToDecimal(SPOutput["id"]),
name = Convert.ToString(SPOutput["name"]),
code = Convert.ToString(SPOutput["code"]),
from = Convert.ToDateTime(SPOutput["from"]),
to = Convert.ToDateTime(SPOutput["to"]),
days = Convert.ToDecimal(SPOutput["days"]),
type_id = convert.YoString(SPOutput["type_id"]),
package = Convert.ToString(SPOutput["package"]),
day = Convert.ToDecimal(SPOutput["day"]),
charge = SPOutput["charge"] as decimal?,
type = Convert.ToString(SPOutput["type"]),
percentage= SPOutput["percentage"] as decimal?,
taxes = Convert.ToDecimal(SPOutput["taxes"]),
order = Convert.ToDecimal(SPOutput["order"]),
level = SPOutput["level"] as decimal?,
Column15 = Convert.ToDecimal(SPOutput[15]),
type_order = (SPOutput["type_order"]) as decimal?,
adults = SPOutput["adults"] as decimal?,
});
var order = outputmodel.OrderBy(c => c.from);
ViewData["RS_Output"] = order;
从MS SQL存储过程中获取输出并存储在viewdata中(按FROM日期排序)。
我的HTML有以下行开始构建表
@foreach (var item in ViewData["RS_Output"] as Enumerable<app.Models.SP_RESULTS.RS_Plans>)
{
//basic <tr> <td> </td> </tr> table setup, using @item.variablename to pull info from the viewdata.
}
我想要实现的输出是针对CODE下的每个TYPE,其中from date =&gt;当前日期,列出房间类型/包裹名称等。
我得到的输出是
我想要得到的是
我认为我需要的是现在的foreach之后的一个foreach,但我不能为我的生活在脑海中找到它。 我改变了
我的控制器中的var order行现在读
var order = outputmodel.OrderBy(c => c.rate);
..我把HTML表创建代码放在if循环中
@foreach (var item in ViewData["RS_Output"] as Enumerable<app.Models.SP_RESULTS.RS_Plans>)
{
if (item.to >= DateTime.now)
{
//basic <tr> <td> </td> </tr> table setup, using @item.variablename to pull info from the viewdata.
}
}
..但是,正如我所说,我很难过。
我想我需要在新创建的if循环中使用另一个foreach,但我无法弄清楚如何。
@foreach (var item in ViewData["RS_Output"] as Enumerable<app.Models.SP_RESULTS.RS_Plans>)
{
if (item.to >= DateTime.now)
{
//other table headers/data
<tr>
<td>
@item.type
</td>
</tr>
<tr>
<td>
Room Type
</td>
<td>
Package / Service
</td>
<td>
Availablility
</td>
<td>
Charge
</td>
<td>
PAX
</td>
<td>
Level
</td>
</tr>
<tr>
==> @foreach (subitem = item.type)
==> {
==> foreach (item.type)
==> {
<td>
@item.type_id
</td>
<td>
@item.package
</td>
<td>
@item.Column15
</td>
<td>
@item.charge
</td>
<td>
@item.adults
</td>
<td>
@item.level
</td>
==> }
==> }
</tr>
}
}
有人可以提出建议吗?
感谢
嗨,我找到的工作是,如果我创建一个名为string previous_type =“”的变量,另一个名为decimal previous_id = 0,那么,在视图中,我可以修改
if (item.to >= item.checkdate)
{
if ((previous_id != item.id) && (previous_type != item.type.ToString()) )
{
//some more code
if (item.type.ToString().Equals(previous_type) == false)
{
previous_type = item.type.ToString();
previous_date_from = item.date_from;
}
//etc
}
感谢大家的帮助
答案 0 :(得分:0)
好的,我想你想要的是先对数据进行分组,然后显示一个表格,然后显示&#39;子表格&#39;对于每个类型的住宿?
如果是这样,那么是的,你可以用嵌套的foreach循环来做到这一点,但你仍然可以更好地强烈地输入你的视图并在控制器中进行分组(或者在某种服务层中可能更好)它可以更容易测试/重复使用)...但是为了让你开始,像这样:
型号:
//Raw data
public class DataRowModel
{
public int Id { get; set; }
public string Class{ get;set;}
public string Description { get; set; }
public DateTime BookingDate { get; set; }
}
//Grouped data
public class GroupedDataRowModel
{
public string Class { get; set; }
public IEnumerable<DataRowModel> Rows { get; set; }
}
//View model
public class DataRowsViewModel
{
public IEnumerable<GroupedDataRowModel> Results { get; set; }
}
控制器操作:
public ActionResult TestData()
{
var PretendDatabaseCall = new List<DataRowModel>
{
new DataRowModel{
Id =1,
BookingDate =new DateTime(2017,1,1),
Description ="Booking 1",
Class="Room"
},
new DataRowModel{
Id =2,
BookingDate =new DateTime(2017,2,1),
Description ="Booking 2",
Class="Room"
},
new DataRowModel{
Id =3,
BookingDate =new DateTime(2017,3,1),
Description ="Booking 3",
Class="Suite"
},
new DataRowModel{
Id =4,
BookingDate =new DateTime(2017,4,1),
Description ="Booking 4",
Class="Room"
},
};
//We can now get the data from the database. We want to group by class so we can
//get a summary of items by class rather than a big flat list. Most LINQ to SQL implementations
//(e.g. Entity Framework) when working with Raw entities could convert this to SQL so the SQL server
//does the grouping, but if not it can happen in memory (get all records, then standard LINQ does it on
//the complete list)
var dataGroupedByClass = PretendDatabaseCall
//Minor Edit: apply filtering here not in the view!
.Where(x=>x.BookingDate >= Datetime.Now)
//Group by class.
.GroupBy(x => x.Class)
//for each class, get the records.
.Select(grpItem => new GroupedDataRowModel()
{
//'key' is the thing grouped by (class)
Class = grpItem.Key,
//grpItem has all the rows within it accessible still.
Rows = grpItem.Select(thisRow => thisRow)
});
var model = new DataRowsViewModel
{
Results = dataGroupedByClass
};
return View("~/Views/Home/TestData.cshtml", model);
}
观看:
@* Strongly typed view. saves any casting back and forth.*@
@model SimpleWeb.Models.DataRowsViewModel
@{
ViewBag.Title = "TestData";
}
<h2>TestData</h2>
<table>
<thead></thead>
<tbody>
@foreach (var groupEntry in Model.Results)
{
@*Add single row with just the class...*@
<tr><td>@groupEntry.Class</td></tr>
@*Header row for each class of booking*@
<tr>
<td>Id</td>
<td>Description</td>
<td>Date</td>
</tr>
foreach (var row in groupEntry.Rows)
{
@*add new row for each actual row*@
<tr>
<td>
@row.Id
</td>
<td>
@row.Description
</td>
<td>
@row.BookingDate
</td>
</tr>
}
}
</tbody>
</table>
这会生成我想要的数据:
Room
Id Description Date
1 Booking 1 01/01/2017 00:00:00
2 Booking 2 01/02/2017 00:00:00
4 Booking 4 01/04/2017 00:00:00
Suite
Id Description Date
3 Booking 3 01/03/2017 00:00:00
显然你想要的房间&#39;和&#39;套房&#39;部分包含更多信息,但这有望帮助您入门?