【发布时间】:2021-08-06 10:26:11
【问题描述】:
我设法显示 2 行复选框,第 2 行最初在 cshtml 视图中被选中。但是在我更改视图上的选择并提交后,发布的数据并未反映当前的选择。它仍然显示页面构建时的初始状态。请帮忙谢谢。
我有一个这样的数据模型:
public class Check
{
public List<CheckModel> chkList = new List<CheckModel>()
{
new CheckModel("1",false),
new CheckModel("2",true)
};
}
public class CheckModel
{
public string name{get;set;}
public bool choice{get;set;}
public CheckModel(string nn, bool bb)
{
name=nn;
choice=bb;
}
}
视图是这样的:
@model 检查
@using (Html.BeginForm("abcde","Home", FormMethod.Post)) {
<ul>
@for(int i=0; i < Model.chkList.Count;i++)
{
<li>
input type="checkbox" asp-for=@Model.chkList[i].choice/>@Model.chkList[i].name
</li>
}
</ul>
<input type="submit" value="submit"/>
}
Homecontroller.cs:
public IActionResult abcde(Check c)
{
return Ok();
}
【问题讨论】:
标签: asp.net-core razor checkbox submit