在Razor Page中设置多个选中值,可以使用多个checkbox来实现。以下是一个示例代码:
在.cshtml页面中:
在.cshtml.cs页面中:
public class MyPageModel : PageModel
{
[BindProperty]
public List SelectedValues { get; set; }
public void OnGet()
{
// 初始化选中值
SelectedValues = new List { "Value1", "Value3" };
}
public IActionResult OnPost()
{
// 处理表单提交
// SelectedValues 包含选中的值
return Page();
}
}
在上述示例中,我们使用SelectedValues
属性作为checkbox的绑定目标,并使用List
作为选中的值的容器。在OnGet
方法中,我们将初始的选中值设置为Value1
和Value3
。在OnPost
方法中,我们可以处理表单提交,并在SelectedValues
属性中获取选中的值。
注意:为了使用asp-for
属性,你需要在你的Razor Page中引入@using Microsoft.AspNetCore.Mvc.Rendering
命名空间。