Как создать несколько таблиц из одного модуля в представлении MVC? - PullRequest
0 голосов
/ 29 мая 2020
• 1000 начало представления, Как разделить представление и показать несколько таблиц, включая заголовок и столбцы?

Это мой код представления:

@model IEnumerable<AljawdahNewSite.Models.Orders_Tables>
@{
    ViewBag.Title = "Details1";
    Layout = "~/Views/Shared/_LayoutPatients.cshtml";
    var ids = new List<int>() { 1, 2, 3, 4 };
}

Тогда это первая таблица:

@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>

}

И вторая таблица:

     @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>

 }

при запуске представления в браузере появляется один заголовок и второй заголовок таблицы, а второй foreach не отображается:

Первая таблица выглядит правильно, как и изображение:

введите описание изображения здесь

Как разделить 2 таблицы и отобразить каждый заголовок и данные в представлении на одной странице?

1 Ответ

0 голосов
/ 02 июня 2020

1- Создайте класс:

public class TablesModel
    {
        public IEnumerable<Model1> model1 { get; set; }

        public IEnumerable<Model2> model2 { get; set; }
    }

2- создайте контроллер, если вы хотите установить связь с использованием идентификатора .Where(), а для использования связи с другой таблицей вы можете использовать .Include():

public ActionResult Index()  
        {  
            var tables = new TablesModel
            {  
                model1 =db.Model1.ToList(),  
                model2 =db.Model2.ToList(),  

            };  
            return View(tables);  

3- создать пустое представление для контроллера и использовать как следующий код:

<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> 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...