c# - 我想同时在两个模型上应用 for-each 循环 Asp.Net MVC

标签 c# asp.net-mvc asp.net-mvc-4 model-view

我想同时在两个模型上申请每个循环。

我的 Controller 操作是:

public ActionResult MarkAttendance(int id)
{
    var students = _context.Students.Where(c => c.ClassId == id).ToList();
    var status = _context.Statuses.ToList();

    var viewModel = new AttendanceViewModel
    {
        Student = students,
        Status = status
    };

    return View(viewModel);
}

我的学生模型是

 public class Student
{
    public int Id { get; set; }

    [Required]
    [StringLength(30)]
    [RegularExpression(@"^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$", ErrorMessage = "Invalid name. Use letters only")]
    public string Name { get; set; }

    [Required]
    [StringLength(30)]
    [RegularExpression(@"^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$", ErrorMessage = "Invalid name. Use letters only")]
    [Display(Name = "Father Name")]
    public String FatherName { get; set; }

    [Required]
    [Range(0, 10000)]
    public int Fee { get; set; }

    [Required]
    [RegularExpression(@"^((\+92)|(0092))-{0,1}\d{3}-{0,1}\d{7}$|^\d{11}$|^\d{4}-\d{7}$", ErrorMessage = "Please enter only mobile number, Landline not acceptable")]
    [Display(Name = "Student Contact No.")]
    public string StudentContact { get; set; }

    [Required]
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Please enter correct phone number")]
    [Display(Name = "Guardian Contact No.")]
    public string GuardianContact { get; set; }

    [Required]
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Enter only numbers")]
    [Display(Name = "Roll Number")]
    [IfRollNoExists]
    public int RollNo { get; set; }

    [Required]
    [Display(Name = "Record Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime DateTime { get; set; }

    [Display(Name = "Student Picture")]
    public String ImageURL { get; set; }

    public Class Class { get; set; }

    [Required]
    [Display(Name = "Class Title")]
    public int ClassId { get; set; }

    [ForeignKey("Account")]
    public int AccountId { get; set; }

    public virtual Account Account { get; set; }


}

我的状态等级是

  public class Status
{
    public int Id { get; set; }

    public string StudentStatus { get; set; }
}

我的 ViewModel 是:

public class AttendanceViewModel
{
    public IEnumerable<Student> Student { get; set; }
    public IEnumerable<Status> Status { get; set; }
}

我的 Razor View 是:

@model  Pioneer.ViewModel.AttendanceViewModel
@foreach (var student in Model.Student)
{
    <tr>
        <td>@student.Name</td>
        <td>@student.RollNo</td>
        <td>
            <div data-toggle="buttons">
                <label class="btn btn-success active">
                    @Html.RadioButtonFor(m => m.Status.StudentStatus, "Present", new { id = "1" })
                </label>
                <label class="btn btn-danger">
                    @Html.RadioButtonFor(m => m.Status.StudentStatus, "Absent", new { id = "2" })
                </label>
                <label class="btn btn-primary">
                    @Html.RadioButtonFor(m => m.Status.StudentStatus, "On Leave", new { id = "3" })
                </label>
                <label class="btn btn-warning">
                    @Html.RadioButtonFor(m => m.Status.StudentStatus, "Short Leave", new { id = "4" })
                </label>
            </div>
        </td>                                           
    </tr>
}

现在当我在使用时遇到错误

@Html.RadioButtonFor(m => m.Status.StudentStatus, "Present", new { id = "1" })

下面我附上了一张错误图片。 Error Screen Shot

请告诉我,如果我在这里使用 HTML 助手,我可以在不使用 for-each 的情况下遍历我的行吗?

最佳答案

您想要实现的目标是不可能的。根据您的 View 代码,Status 模型应该是 Student 模型类类的一部分。

因此您的 View 模型将如下所示:

public class AttendanceViewModel
{
    public IEnumerable<Student> Student { get; set; }
}

Student 模型类将包含状态:

public class Student
{
    public IEnumerable<Status> Status { get; set; }
}

现在您可以像这样访问 View 中的状态:

@model  Pioneer.ViewModel.AttendanceViewModel       
@foreach (var student in Model.Student)
{
    <tr>
        <td>@student.Name</td>
        <td>@student.RollNo</td>

        @foreach(var status in student)
        {
            <td>
                <div data-toggle="buttons">
                    <label class="btn btn-success active">
                        @Html.RadioButtonFor(status.StudentStatus, "Present", new { id = "1" })
                    </label>
                    <label class="btn btn-danger">
                        @Html.RadioButtonFor(statusStudentStatus, "Absent", new { id = "2" })
                    </label>
                    <label class="btn btn-primary">
                        @Html.RadioButtonFor(status.StudentStatus, "On Leave", new { id = "3" })
                    </label>
                    <label class="btn btn-warning">
                        @Html.RadioButtonFor(status.StudentStatus, "Short Leave", new { id = "4" })
                    </label>
                </div>
            </td>  
        }
    </tr>
}

编辑:

或者,如果您特别希望使用与问题中提到的相同的 ViewModel,则可以将 foreach 替换为 for。检查下面的示例代码:

@model  Pioneer.ViewModel.AttendanceViewModel
@for(var studentIndex = 0; studentIndex < Model.Student.Count(); studentIndex++)
{
    <tr>
        <td>@Model.Student[studentIndex].Name</td>
        <td>@Model.Student[studentIndex].RollNo</td>

        @for(var statusIndex = 0; statusIndex < Model.Status.Count; statusIndex++)
        {
            <td>
                <div data-toggle="buttons">
                    <label class="btn btn-success active">
                        @Html.RadioButtonFor(m => m.Status[statusIndex].StudentStatus, "Present", new { id = "1" })
                    </label>
                    <label class="btn btn-danger">
                        @Html.RadioButtonFor(m => m.Status[statusIndex].StudentStatus, "Absent", new { id = "2" })
                    </label>
                    <label class="btn btn-primary">
                        @Html.RadioButtonFor(m => m.Status[statusIndex].StudentStatus, "On Leave", new { id = "3" })
                    </label>
                    <label class="btn btn-warning">
                        @Html.RadioButtonFor(m => m.Status[statusIndex].StudentStatus, "Short Leave", new { id = "4" })
                    </label>
                </div>
            </td>     
        }       
    </tr>
}

关于c# - 我想同时在两个模型上应用 for-each 循环 Asp.Net MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44194102/

相关文章:

c# - 有没有正确的方法来检索 SQL 中的字符串字段

javascript - Array.Push 仅返回最后推送的对象。如何解决?

asp.net - 布局问题 CSS

C#访问静态函数中的非静态成员

c# - ASP.NET Core 2 - 缺少内容类型边界

asp.net - 是否可以访问另一个项目中的MVC View ?

jquery - 使用jquery ajax调用导出到excel

c# - 为什么 .net mvc 4 web api 服务在异常消息中显示本地计算机的路径?

c# - LINQ 在 XML 文件中创建新记录

asp.net-mvc - asp.net mvc Html.ActionLink() 保留我不想要的路由值