我是ASP.NET MVC的新手,有一个特定的问题需要您的帮助。
我正在处理学校申请。该应用程序能够在数据库中注册学生,校长(管理员(可以将他们分配到组中。
这是控制器的代码
public async Task<IActionResult> GroupInfo(int id, int grade)
{
var groupVM = new GroupViewModel();
groupVM.Id = id;
groupVM.Grade = grade;
foreach (var stud in await userManager.Users.Where(s => s.ApplicationUserType == ApplicationUserType.Student && s.Status == false).ToListAsync())
{
var studentViewModel = new StudentInfoViewModel
{
Id = stud.Id,
FirstName = stud.FirstName,
LastName = stud.LastName,
Email = stud.Email,
PreviousGrade = stud.Comments,
IsSelected = stud.Status
};
groupVM.Students.Add(studentViewModel);
}
ViewBag.Students = groupVM.Students.Where(s => s.PreviousGrade == groupVM.Grade.ToString()).Select(s => new SelectListItem()
{
Text = s.FirstName + " " + s.LastName + ", Previous grade: " + s.PreviousGrade,
Value = s.Id
}).ToList();
return View(groupVM);
}
控制器将免费学生分配到组中,但只显示与该组成绩匹配的学生。在我看来,我需要对那些与小组成绩不匹配的学生进行验证。
我想在我看来进行检查,这是代码
<form method="post" asp-action="GroupInfo" asp-controller="PrincipalProfile" class="form-horizontal" role="form">
<div class="card">
<div class="card-header">
<h2>Add or remove students to current group</h2>
</div>
<div class="card-body">
<div class="form-group">
<label class="control-label">List of Students</label>
@if (ViewBag.Students != 0)
{
<select asp-for="StudentId" class="form-control" asp-items="ViewBag.Students">
<option selected disabled>Choose a student to add</option>
</select>
}
else
{
<span>There are no available students at this grade.</span>
}
</div>
</div>
<div class="card-footer">
<input type="submit" value="Update" class="btn btn-primary" />
<a asp-controller="PrincipalProfile" asp-action="Groups" class="btn btn-dark">Cancel</a>
</div>
</div>
当我试图检查viewbag是否包含信息时,我会得到RuntimeBinderException。
RuntimeBinderException:运算符'!='不能应用于类型为"System.Collections.Generic.List<Microsoft.AspNetCore.Mvc.渲染。选择ListItem>'和"int">
如果你能分享你对如何解决这个问题的看法和看法,我将不胜感激。
提前谢谢。
试试这个
@if ( ViewBag.Students!=null && ViewBag.Students.Count > 0)