C# 我想同时在两个模型上应用每个循环Asp.NETMVC
我想同时申请两个模型上的每个循环 我的控制器操作是:C# 我想同时在两个模型上应用每个循环Asp.NETMVC,c#,asp.net-mvc,asp.net-mvc-4,model-view,C#,Asp.net Mvc,Asp.net Mvc 4,Model View,我想同时申请两个模型上的每个循环 我的控制器操作是: public ActionResult MarkAttendance(int id) { var students = _context.Students.Where(c => c.ClassId == id).ToList(); var status = _context.Statuses.ToList(); var viewModel = new AttendanceViewModel {
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 AttendanceViewModel
{
public IEnumerable<Student> Student { get; set; }
public IEnumerable<Status> Status { get; set; }
}
@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>
}
public class AttendanceViewModel
{
public IEnumerable<Student> Student { get; set; }
}
public class Student
{
public IEnumerable<Status> Status { get; set; }
}
@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>
}
我的学生模型是
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; }
}
我的身份类别是
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; }
}
我的视图模型是:
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 AttendanceViewModel
{
public IEnumerable<Student> Student { get; set; }
public IEnumerable<Status> Status { get; set; }
}
@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>
}
public class AttendanceViewModel
{
public IEnumerable<Student> Student { get; set; }
}
public class Student
{
public IEnumerable<Status> Status { get; set; }
}
@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>
}
下面我附上了错误的图片
如果我在这里使用HTML helper,请告诉我是否可以在不使用for each的情况下迭代我的行?您试图实现的目标是不可能的。根据您的查看代码,
状态
模型应该是学生
模型类的一部分
因此您的视图模型将如下所示:
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 AttendanceViewModel
{
public IEnumerable<Student> Student { get; set; }
public IEnumerable<Status> Status { get; set; }
}
@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>
}
public class AttendanceViewModel
{
public IEnumerable<Student> Student { get; set; }
}
public class Student
{
public IEnumerable<Status> Status { get; set; }
}
@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>
}
m.Status
itsIEnumerable
,它没有StudentStatus
属性。
@Html.RadioButtonFor(m => m.Status.StudentStatus, "Absent", new { id = "2" })
首先,viewmodel名称和类名都处于状态,请更改它,然后重试。你确定StudentStatus在Status中吗?因为没有对modelu的描述。这里有一些不正确的逻辑。请明确说明你想要实现的目标。@anete.anetes我更新了我的问题。我已经测试了两种解决方案,在我这方面似乎效果很好。你试过答案中提到的两种解决方案吗?你现在犯了什么错误@Alamzaibfarookin在嵌套循环中,它给出了
student
不可枚举的错误。对于每个循环,无法迭代Pioneer.Models.StudentMy Controllerpublic ActionResult MarkAttention(int id){var students=_context.students.Where(c=>c.ClassId==id.ToList();var status=_context.Statuses.ToList();var viewModel=new attentinceviewmodel{Student=students};返回视图(viewModel);}
我解决了嵌套循环中的问题。现在我无法访问状态
属性。当我在单选按钮状态中写入时。StudentStatus
无法识别StudentStatus
你能用我提到的@方法而不是@foreach
方法检查一下吗?@AlamzaibFarooq