使用 Tag Helper 绑定复选框列表
Bind Checkboxlist using Tag Helper
我正在尝试使用标签助手绑定复选框列表。问题是我的复选框列表总是 return false。如果我使用 request.form["Category"] 而不是标签助手,它就会起作用。我的问题在哪里。谢谢
型号:
public partial class Policy
{
public int Id { get; set; }
public string Category { get; set; }
[NotMapped]
public List<PolicyCategory> Categories { get; set; }
}
public class PolicyCategory
{
[Key]
public int Category_id { get; set; }
public string Category_name { get; set; }
[NotMapped]
public bool Selected { get; set; }
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Category")] Policy policy)
{
var checkedString = string.Empty;
List<PolicyCategory> Categories = PopulateCategories();
for (int i = 0; i < Categories.Count(); i++)
{
if (Categories[i].Selected==true)
{
checkedString += Categories[i].Category_name + ",";
}
}
...
return View(policy);
}
查看:
for (int i=0; i<Model.Categories.Count(); i++)
{
<input type="checkbox" asp-for="@Model.Categories[i].Selected" />
<label asp-
for="@Model.Categories[i].Selected">@Model.Categories[i].Category_name</label>
<input type="hidden" asp-for="@Model.Categories[i].Category_id" />
<input type="hidden" asp-for="@Model.Categories[i].Category_name" />
<br />
}
}
Model:
public partial class Policy
{
public int Id { get; set; }
public List<PolicyCategory> Category { get; set; }
[NotMapped]
public List<PolicyCategory> Categories { get; set; }
}
View:
for (int i=0; i<Model.Categories.Count(); i++)
{
<input type="checkbox" asp-for="@Model.Categories[i].Selected" />
<label asp-for="@Model.Categories[i].Selected">@Model.Categories[i].Category_name</label>
<input type="hidden" asp-for="@Model.Categories[i].Category_id" />
<input type="hidden" asp-for="@Model.Categories[i].Category_name" />
<br />
}
}
我正在尝试使用标签助手绑定复选框列表。问题是我的复选框列表总是 return false。如果我使用 request.form["Category"] 而不是标签助手,它就会起作用。我的问题在哪里。谢谢
型号:
public partial class Policy
{
public int Id { get; set; }
public string Category { get; set; }
[NotMapped]
public List<PolicyCategory> Categories { get; set; }
}
public class PolicyCategory
{
[Key]
public int Category_id { get; set; }
public string Category_name { get; set; }
[NotMapped]
public bool Selected { get; set; }
}
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Category")] Policy policy)
{
var checkedString = string.Empty;
List<PolicyCategory> Categories = PopulateCategories();
for (int i = 0; i < Categories.Count(); i++)
{
if (Categories[i].Selected==true)
{
checkedString += Categories[i].Category_name + ",";
}
}
...
return View(policy);
}
查看:
for (int i=0; i<Model.Categories.Count(); i++)
{
<input type="checkbox" asp-for="@Model.Categories[i].Selected" />
<label asp-
for="@Model.Categories[i].Selected">@Model.Categories[i].Category_name</label>
<input type="hidden" asp-for="@Model.Categories[i].Category_id" />
<input type="hidden" asp-for="@Model.Categories[i].Category_name" />
<br />
}
}
Model:
public partial class Policy
{
public int Id { get; set; }
public List<PolicyCategory> Category { get; set; }
[NotMapped]
public List<PolicyCategory> Categories { get; set; }
}
View:
for (int i=0; i<Model.Categories.Count(); i++)
{
<input type="checkbox" asp-for="@Model.Categories[i].Selected" />
<label asp-for="@Model.Categories[i].Selected">@Model.Categories[i].Category_name</label>
<input type="hidden" asp-for="@Model.Categories[i].Category_id" />
<input type="hidden" asp-for="@Model.Categories[i].Category_name" />
<br />
}
}