C# 在列表中为模型中的字符串是否为空生成复选框<;模型>;内部视图模型

C# 在列表中为模型中的字符串是否为空生成复选框<;模型>;内部视图模型,c#,asp.net-mvc,razor,viewmodel,C#,Asp.net Mvc,Razor,Viewmodel,我有一个viewmodel,它包含一个从数据库中收集的带有string属性的模型的icollection。我试图从表中的viewmodel中为这些模型编制索引,但我不想写出字符串,而是想使用razor生成一个复选框,确定字符串是否为null。有没有一个简单的方法来做到这一点,我没有看到?我不断得到空引用错误或什么都没有 这是我的viewmodel: public class CourseViewModel { public int CourseId { get; set; }

我有一个viewmodel,它包含一个从数据库中收集的带有string属性的模型的icollection。我试图从表中的viewmodel中为这些模型编制索引,但我不想写出字符串,而是想使用razor生成一个复选框,确定字符串是否为null。有没有一个简单的方法来做到这一点,我没有看到?我不断得到空引用错误或什么都没有

这是我的viewmodel:

public class CourseViewModel
{
    public int CourseId { get; set; }

    [Display(Name = "Course Name")]
    public string CourseName { get; set; }

    public ICollection<NewPage> CoursePages { get; set; }
}
公共类CourseView模型
{
public int CourseId{get;set;}
[显示(Name=“课程名称”)]
公共字符串CourseName{get;set;}
公共ICollection CoursePages{get;set;}
}
以下是我的看法:

@foreach (var item in Model.CoursePages)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.PageNumber)
    </td>
    @*<td>
        @if (item.DrillInstructions == null)
        {
            Html.CheckBox("Instructions", false);
        }
        @if (item.DrillInstructions != null)
        {
            Html.CheckBox("Instructions", true);
        }
    </td>*@
    <td>
        @Html.DisplayFor(modelItem => item.DrillInstructions)
    </td>
@foreach(Model.CoursePages中的变量项)
{
@DisplayFor(modeleItem=>item.Title)
@DisplayFor(modelItem=>item.PageNumber)
@*
@if(item.DrillInstructions==null)
{
复选框(“说明”,false);
}
@if(item.DrillInstructions!=null)
{
复选框(“说明”,true);
}
*@
@DisplayFor(modelItem=>item.DrillInstructions)

如果只是为了显示,您可以使用字形图标或类似的图标

<td>
    <i class='fa @(item.DrillInstructions == null ? "fa-square" : "fa-checksquare")'></i>
</td>


使用fa-2x可以显示更大的复选框。

如果只是为了显示,可以使用字形图标或类似的图标

<td>
    <i class='fa @(item.DrillInstructions == null ? "fa-square" : "fa-checksquare")'></i>
</td>

对于较大的复选框,请使用fa-2x