如何分隔2个表,并在一页视图中分别显示每个标题和数据?解决方案 1-创建类: 公共类TablesModel{公共IEnumerable< Model1>model1 {获取;放;}公共IEnumerable< Model2>model2 {获取;放;}} 2-如果要与ID建立关系,请使用 .Where()创建控制器,并与另一个表建立关系,可以使用 .Include(): 公共ActionResult Index(){var table = new TablesModel{model1 = db.Model1.ToList(),model2 = db.Model2.ToList(),};返回View(tables); 3-为控制器创建一个空视图,并使用如下代码: < div class ="tab-content">< h5 class ="text-uppercase p-2 text-center">模型列表</h5>< table class ="table table-bordered table-striped">< thead class ="thead-dark text-white">< tr>< th> @ Html.DisplayNameFor(m => Model.model1.FirstOrDefault().Name)</th;< th> @ Html.DisplayNameFor(m => Model.model1.FirstOrDefault().Age)</th;</tr></thead>< tbody>@foreach(Model.model1中的var项){< tr>< td> @ item.Name</td>< td> @ item.Age</td></tr>}</tbody></table></div>< div class ="tab-content">< h5 class ="text-uppercase p-2 text-center">模型列表</h5>< table class ="table table-bordered table-striped">< thead class ="thead-dark text-white">< tr>< th> @ Html.DisplayNameFor(m => Model.model2.FirstOrDefault().Address)</th;< th> @ Html.DisplayNameFor(m => Model.model2.FirstOrDefault().Mobile)</tr></thead>< tbody>@foreach(Model.model2中的var项){< tr>< td> @ item.Address</td>< td> @ item.Mobile</td></tr>}</tbody></table></div> I need to create multiple tables from one module in mvc view , I created 2 header and 2 foreach but when i run the view its not seperated and second set of columns header not appeared only one set of header columns at the beginning of view , How to seperate the view and show multiple tables include header and columns ?This is my View code :@model IEnumerable<AljawdahNewSite.Models.Orders_Tables>@{ ViewBag.Title = "Details1"; Layout = "~/Views/Shared/_LayoutPatients.cshtml"; var ids = new List<int>() { 1, 2, 3, 4 };}Then this is first table :@if (ids.Contains(Model.First().labCashView.DEPTID.GetValueOrDefault())){ <div class="tab-content"> <div class="tab-pane container active p-0" style="margin-left:15px"> <hr /> <dl class="horizontal" style="border:solid"> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Patient_Name)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Patient_Name)</dd> <dt style="width: 22%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Customer_Name)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Customer_Name)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Patient_No)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Patient_No)</dd> <dt style="width: 22%;display: inline-block;margin-left:0px;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Collection_Date)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Collection_Date)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.SEX)</dt> <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.SEX)</dd> <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Receiving_Date)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Receiving_Date)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.AGE)</dt> <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.AGE)</dd> <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Report_Date)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Report_Date)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.order_number)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.order_number)</dd> </dl> <table id="menu" class="table table-bordered table-striped"> <tr> <td> test name </td> <td> result </td> <td> From Range </td> <td> To Range </td> <td> Other Range </td> <td> Report Date </td> </tr> <tbody> @foreach (var item in Model.Where(x => ids.Contains(x.labCashView.DEPTID.GetValueOrDefault()))) { <tr> <td>@item.labCashView.Test_Name</td> <td>@item.labCashView.Result</td> <td>@item.labCashView.Low_Range</td> <td>@item.labCashView.High_Range</td> <td style="width:20%">@item.labCashView.Text_Range</td> <td>@item.labCashView.Report_Date.Value.ToShortDateString()</td> </tr> } </tbody> </table> <hr /> </div> </div>}And second table : @if (Model.First().labCashView.DEPTID == 6) { <div class="tab-content"> <div class="tab-pane container fade p-0" style="margin-left:15px"> <hr /> <dl class="horizontal" style="border:solid"> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Patient_Name)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Patient_Name)</dd> <dt style="width: 22%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Customer_Name)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Customer_Name)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Patient_No)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Patient_No)</dd> <dt style="width: 22%;display: inline-block;margin-left:0px;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Collection_Date)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Collection_Date)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.SEX)</dt> <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.SEX)</dd> <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Receiving_Date)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Receiving_Date)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.AGE)</dt> <dd style="width: 20%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.AGE)</dd> <dt style="width: 22%;display: inline-block;margin-left:60px;color:blue;">@Html.DisplayNameFor(model => model.labCashView.Report_Date)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:0px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.Report_Date)</dd> <dt style="width: 20%;display: inline-block;color:blue;">@Html.DisplayNameFor(model => model.labCashView.order_number)</dt> <dd style="width: 25%;display: inline-block;margin: 0px;margin-left:-50px">@Html.DisplayFor(model => model.FirstOrDefault().labCashView.order_number)</dd> </dl> <table id="menu1" class="table table-bordered table-striped"> <tr> <td> test name </td> <td> result </td> <td> From Range </td> <td> To Range </td> <td> Other Range </td> <td> Report Date </td> </tr> <tbody> @foreach (var item in Model.Where(x => x.labCashView.DEPTID == 6)) { <tr> <td>@item.labCashView.TEXT</td> <td>@item.labCashView.TEST_RESULT</td> <td>@item.labCashView.UNIT</td> <td>@item.labCashView.Text_Range</td> <td>@item.labCashView.Report_Date.Value.ToShortDateString()</td> </tr> } </tbody> </table> <hr /> </div> </div> }when run the view in browser its appear one header and one table second header and second foreach not appeared :First table appeared correct like the image :How to separate 2 tables and show each header and data in view each in one page ? 解决方案 1- Create the class :public class TablesModel { public IEnumerable<Model1> model1 { get; set; } public IEnumerable<Model2> model2 { get; set; } }2- create the controller if you want to make relation with id use .Where()and to use relation with another table you can use .Include():public ActionResult Index() { var tables = new TablesModel { model1 =db.Model1.ToList(), model2 =db.Model2.ToList(), }; return View(tables);3- create empty view for controller and use like the following code :<div class="tab-content"> <h5 class="text-uppercase p-2 text-center">model list</h5> <table class="table table-bordered table-striped"> <thead class="thead-dark text-white"> <tr> <th>@Html.DisplayNameFor(m => Model.model1.FirstOrDefault().Name)</th> <th>@Html.DisplayNameFor(m => Model.model1.FirstOrDefault().Age)</th> </tr> </thead> <tbody> @foreach (var item in Model.model1 ) { <tr> <td>@item.Name</td> <td>@item.Age</td> </tr> } </tbody> </table> </div><div class="tab-content"> <h5 class="text-uppercase p-2 text-center">model list</h5> <table class="table table-bordered table-striped"> <thead class="thead-dark text-white"> <tr> <th>@Html.DisplayNameFor(m => Model.model2.FirstOrDefault().Address)</th> <th>@Html.DisplayNameFor(m => Model.model2.FirstOrDefault().Mobile)</th> </tr> </thead> <tbody> @foreach (var item in Model.model2 ) { <tr> <td>@item.Address</td> <td>@item.Mobile</td> </tr> } </tbody> </table> </div> 这篇关于如何在MVC视图中的一个模块中创建多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 22:56
查看更多