我想在mvc。
中绑定checkboxlist我也完成了代码,但是我在运行网站时遇到了错误。
这是我的代码,
我创建了模型
public class StaffRegistration
{
public IList<SelectListItem> WeekEndDay { get; set; }
}
这是我的DBContext
public class TableContext : DbContext
{
public DbSet<StaffRegistration> staff { get; set; }
}
这是我的控制器
public class StaffRegistrationController : Controller
{
public ActionResult Weekend()
{
List<SelectListItem> names = new List<SelectListItem>();
names.Add(new SelectListItem { Text = "Monday", Value = "1" });
names.Add(new SelectListItem { Text = "Tuesday", Value = "2" });
names.Add(new SelectListItem { Text = "Wednesday", Value = "3" });
names.Add(new SelectListItem { Text = "Thrusday", Value = "4" });
names.Add(new SelectListItem { Text = "Friday", Value = "5" });
names.Add(new SelectListItem { Text = "Saturday", Value = "6" });
names.Add(new SelectListItem { Text = "Sunday", Value = "7" });
staff.WeekEndDay = names;
return View(staff);
}
}
最后这是我的观点
@model SchoolWebApplication.Models.StaffRegistration
@foreach (var names in @Model.WeekEndDay)
{
var checkBoxId = "chk" + names.Value;
var tdId = "td" + names.Value;
<table width="100%">
<tr>
<td width="20px">
<input type="checkbox"
id="@checkBoxId"
class="chkclass" value="@names.Value" />
</td>
<td id="@tdId" width="100px">
@names.Text
</td>
</tr>
</table>
}
所以,这是用于绑定周末复选框列表的代码,但当我在本地运行网站时,它显示错误,即:
"EntityType 'SelectListItem' has no key defined. Define the key for this EntityType.
SelectListItems: EntityType: EntitySet 'SelectListItems' is based on type 'SelectListItem' that has no keys defined."
为了更清楚,我也更新了图像。请查看它。
谢谢。