我有兩張桌子PK和FK。
PK表記錄:- 主題
**SubId** Name
1 XYZ
2 PQR
3 ABC
4 TTR
5 HGF
FK 表記錄:- 學生
StuId Name **SubId**
1 STU1 4
2 STU2 4
3 STU3 4
4 STU4 2
5 STU5 1
現在,當我在視圖中顯示 SUBJECT TABLE 時,如果學生表中存在受尊重的主題 ID,我想隱藏洗掉按鈕。
所需的視圖設計
SubId Name Action
1 XYZ
2 PQR
3 ABC DELETE
4 TTR
5 HGF DELETE
在行號3 和 5中顯示洗掉按鈕,但不在 1,2 和 4 中。
這是我在其中獲取串列的操作結果代碼。
public IActionResult Index()
{
var countobj = new CountRecord();
countobj.objSubjectList = _wOService.SubjectList();
countobj.objStudentlist = _wOService.CountSubjectandStudent();
return View(countobj);
}
方法代碼_wOService.SubjectList(); 回傳串列
public List<Subject> SubjectList()
{
...
string sql = @"select * from tblSubject";
...
}
方法代碼_wOService.CountSubjectandStudent(); 回傳串列
public List<Student> CountSubjectandStudent()
{
...
select Subject.ID from Subject inner join Student on Subject.ID=Student.SubId
GROUP BY Subject.ID
...
}
班級代碼
public class CountRecord
{
public List<Subject> objSubjectList { get; set; }
public List<Student> objStudentlist { get; set; }
}
查看頁面代碼
@model XXXXXX.CountRecord
@foreach (var item in Model.objSubjectList )
{
<tr id="@item.ID">
<td>@item.ID</td>
<td>@item.Name</td>
@foreach (var itemDisplay in Model.objStudentlist)
{
if (item.ID== Convert.ToString(itemDisplay.SubId)) {
<td><a class='btn btn-danger' style="color:white"
onclick="DeleteSubject(PASSID);">Delete</a>
}
}
</td>
</tr>
}
uj5u.com熱心網友回復:
“現在當我在視圖中顯示 SUBJECT TABLE 時,如果學生表中存在受尊重的主題 ID,我想隱藏洗掉按鈕?”
還有更好、更優雅、更簡單的方法來處理您嘗試實作的內容。這樣做的演算法如下。
演算法
============
Algorithm
============
1.Find the list Of subject where student has no enrolment
2.Loop over the list of subject and check which subject has no enrolment
3.Set "no enrolment" to a new ViewModel and Build new List
4.Get the new list of enrolment status and set button into it
5.Repeat 2 to 4
模型
public class Subject
{
public int SubId { get; set; }
public string SubName { get; set; }
}
public class Student
{
public int StuId { get; set; }
public string StuName { get; set; }
public string SubId { get; set; }
}
查看您需要的模型
public class StudentSubjectViewModel
{
public int SubId { get; set; }
public string SubName { get; set; }
public bool IsDelete { get; set; }
}
控制器
public IActionResult Index()
{
/*
================
Implementation
================
*/
//1.Find the list Of subject where student has no enrolment
var subThatStudentDont = ListOfSubject.Where(stu => ListOfStudent.All(sub => sub.SubId.ToString() != stu.SubId.ToString()));
//Building new viewModel for Final output
List<StudentSubjectViewModel> viewModelList = new List<StudentSubjectViewModel>();
//2.Loop over the list of subject and check which subject has no enrolment
foreach (var item in ListOfSubject)
{
var studentSubjectViewModel = new StudentSubjectViewModel
{
SubId = item.SubId,
SubName = item.SubName,
IsDelete = subThatStudentDont.Any(x => x.SubId == item.SubId) ? true : false //3.Set "no enrolment" to a new ViewModel and Build new List
};
//5.Repeat 1 to 4
viewModelList.Add(studentSubjectViewModel);
};
return View(viewModelList);
}
注意:這里的重點是
subThatStudentDont.Any(x => x.SubId == item.SubId) ? true : false我們正在檢查Ternary operator學生是否具有特定的主題 ID,并將狀態設定為 true 或 false。
看法
@model IEnumerable<DotNet6MVCWebApp.Models.StudentSubjectViewModel>
@{
ViewData["Title"] = "Index";
}
<h2>Student Subject</h2>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.SubId)
</th>
<th>
@Html.DisplayNameFor(model => model.SubName)
</th>
<th>
@Html.DisplayNameFor(model => model.IsDelete)
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.SubId)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsDelete)
</td>
<td>
@{
if (item.IsDelete)
{
<a asp-action="Delete" class="btn btn-danger" asp-route-subId="@item.SubId">Delete</a>
}
}
</td>
</tr>
}
</tbody>
</table>
注意:這里我們正在檢查我們之前設定的
subject statusbyif(item.IsDelete)并顯示預期的輸出。
輸出


希望它會相應地指導您,您正在嘗試實施什么。
uj5u.com熱心網友回復:
您不應該像這樣轉換為字串:
if (item.ID== Convert.ToString(itemDisplay.SubId))
更正如下:
if (item.ID== itemDisplay.SubId)
如果它不起作用,你能提供Subject和Student類嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/485649.html
