如果子项中存在值,则隐藏按钮 table

Hide button if value is present in child table

我有两个tablePK和FK。

PK Table 记录:- 主题

**SubId**    Name
1        XYZ
2        PQR
3        ABC
4        TTR
5        HGF

FK Table 记录:- 学生

StuId     Name    **SubId**  
1         STU1    4
2         STU2    4
3         STU3    4
4         STU4    2
5         STU5    1    

现在,当我在视图中显示主题 TABLE 时,如果学生 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();return列表

public List<Subject> SubjectList()
{
...
string sql = @"select * from tblSubject";
...
}

方法代码_wOService.CountSubjectandStudent();return列表

public List<Student> CountSubjectandStudent()
{
...
select Subject.ID from Subject inner join Student on Subject.ID=Student.SubId
GROUP BY Subject.ID 
...
}

CLASS代码

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

你不应该像这样转换成字符串:

if (item.ID== Convert.ToString(itemDisplay.SubId))

更正如下:

if (item.ID== itemDisplay.SubId)

万一不行,能否请您提供SubjectStudent类?

“现在,当我在视图中显示主题 TABLE 时,如果受尊重的主题 ID 存在于学生 table 中,我想隐藏删除按钮?”

There is even better, elegant and simplest way to handle what you are trying to implement. The algorithm of doing that would be like as following.

算法

============
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);
        }

Note: Here the point is subThatStudentDont.Any(x => x.SubId == item.SubId) ? true : false we are checking with the Ternary operator Whether the student has that particular subject id or not and setting the status as true or 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>

Note: Here we are checking the subject status by if(item.IsDelete) which we have set earlier and displaying the expected output.

输出

希望它能相应地指导你,你正在努力实现什么。