C# Asp Net核心单选按钮集合
这是整个UI的razor视图,基本上它是一种为每个项目选择3个选项的方法,每个项目都属于一个组,但每个项目都有一个唯一的IdC# Asp Net核心单选按钮集合,c#,html,asp.net-core,razor-pages,C#,Html,Asp.net Core,Razor Pages,这是整个UI的razor视图,基本上它是一种为每个项目选择3个选项的方法,每个项目都属于一个组,但每个项目都有一个唯一的Id <form id="form" method="post"> <div class="card"> <div class="card-header bg-white"> <div class="card-title text-dark"><b>@Model.Ce
<form id="form" method="post">
<div class="card">
<div class="card-header bg-white">
<div class="card-title text-dark"><b>@Model.Celula.Nome</b></div>
</div>
<div class="card-body">
<div class="alert alert-danger" style="display:none;">
<h4 class="alert-heading"><i class="fa fa-exclamation-triangle"></i> Atenção</h4>
<p> É necessário selecionar uma opção por item</p>
</div>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
@foreach (var grupo in Model.Celula.CheckList.Grupos)
{
<a class="nav-item nav-link " data-toggle="tab" href="#@grupo.Nome" role="tab">@grupo.Nome</a>
}
<a class="nav-item nav-link last-tab" data-toggle="tab" href="#finalizar" role="tab">Finalizar</a>
</div>
<div class="tab-content mt-3">
<input hidden="hidden" asp-for="Celula.CheckList.Id" />
@for (int i = 0; i < Model.Celula.CheckList.Grupos.Count(); i++)
{
<input hidden="hidden" asp-for="Celula.CheckList.Grupos[i].Id" />
<div class="tab-pane fade" id="@Model.Celula.CheckList.Grupos[i].Nome" role="tabpanel">
<table class="table table-bordered table-sm">
<thead>
<tr>
<th class="text-center">@Model.Celula.CheckList.Grupos[i].Nome</th>
<th class="text-center">Sim</th>
<th class="text-center">Não</th>
<th class="text-center">Não Aplicável</th>
</tr>
</thead>
<tbody>
@for (int j = 0; j < Model.Celula.CheckList.Grupos[i].Items.Count(); j++)
{
<tr>
<th class="pl-4">
<i class="fas fa-caret-right"></i>
<input type="hidden" asp-for="Celula.CheckList.Grupos[i].Items[j].Id" />
<span class="ml-3">@Model.Celula.CheckList.Grupos[i].Items[j].Nome</span>
</th>
<td class="text-center item">
<input type="radio" name="@($"item{Model.Celula.CheckList.Grupos[i].Items[j].Id}")" value="S" required style="display:none;" />
<i class="fa fa-check" style="display:none;"></i>
</td>
<td class="text-center item">
<input type="radio" name="@($"item{Model.Celula.CheckList.Grupos[i].Items[j].Id}")" value="N" required style="display:none;" />
<i class="fa fa-check" style="display:none;"></i>
</td>
<td class="text-center item">
<input type="radio" name="@($"item{Model.Celula.CheckList.Grupos[i].Items[j].Id}")" value="NA" required style="display:none;" />
<i class="fa fa-check" style="display:none;"></i>
</td>
</tr>
}
</tbody>
</table>
<div class="form-row">
<div class="col-6">
<button type="button" class="btn btn-outline-primary prev" disabled> <i class="fa fa-arrow-left"></i> </button>
<button type="button" class="btn btn-outline-primary next"> <i class="fa fa-arrow-right"></i> </button>
</div>
</div>
</div>
}
<div class="tab-pane fade" id="finalizar" role="tabpanel">
<div class="form-row">
<div class="col-12">
<div class="alert alert-info" style="display:none;">
<h4 class="alert-heading"><i class="fa fa-exclamation-circle"></i> Atenção</h4>
<p> Ainda tem itens por validar! Não será possível Validar</p>
</div>
</div>
</div>
<div class="form-row">
<div class="col-6">
<button type="button" class="btn btn-outline-primary prev"> <i class="fa fa-arrow-left"></i> </button>
<button type="button" onclick="onSubmit()" id="submitButton" class="btn btn-outline-primary"><i class="fas fa-check"></i> Validar</button>
</div>
</div>
</div>
</div>
</div>
<div class="card-footer">
</div>
</div>
</form>
@Model.Celula.Nome
阿滕ção
Énecessário selecionar uma opço por项目
@foreach(Model.Celula.CheckList.grupo中的var grupo)
{
}
@对于(int i=0;i
瓦利达尔
页面模型
public class IndexModel : PageModel
{
private readonly DatabaseContext _context;
private readonly IMapper _mapper;
public IndexModel(DatabaseContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
#region Properties
[BindProperty]
public CelulaViewModel Celula { get; set; }
[BindProperty]
public Dictionary<int, string> Values { get; set; }
#endregion
#region Handlers
public IActionResult OnGet(int? id)
{
if (id == null)
{
return NotFound();
}
var celula = _context.Celulas
.Include(c => c.CheckList)
.ThenInclude(cl => cl.Grupos)
.ThenInclude(cl => cl.Items)
.Where(c => c.Automated)
.FirstOrDefault(c => c.Id == id);
Celula = _mapper.Map<Celula, CelulaViewModel>(celula);
if (Celula == null)
{
return NotFound();
}
return Page();
}
public IActionResult OnPost()
{
return RedirectToPage("../Index");
}
public class CelulaViewModel
{
public int Id { get; set; }
public string Nome { get; set; }
public int? CheckListId { get; set; }
public CheckListViewModel CheckList { get; set; }
}
public class CheckListViewModel
{
public int Id { get; set; }
public string Nome { get; set; }
public IList<CheckListGrupoViewModel> Grupos { get; set; }
public IList<CelulaViewModel> Celulas { get; set; }
}
public class CheckListGrupoViewModel
{
public int Id { get; set; }
public string Nome { get; set; }
public IList<CheckListGrupoItemViewModel> Items { get; set; }
}
public class CheckListGrupoItemViewModel
{
public int Id { get; set; }
public string Nome { get; set; }
public string Value { get; set; }
public int GrupoId { get; set; }
public CheckListGrupoViewModel Grupo { get; set; }
}
公共类索引模型:PageModel
{
私有只读数据库上下文\u上下文;
专用只读IMapper\u映射器;
公共索引模型(DatabaseContext上下文,IMapper映射器)
{
_上下文=上下文;
_映射器=映射器;
}
#区域属性
[BindProperty]
公共CelulaViewModel Celula{get;set;}
[BindProperty]
公共字典值{get;set;}
#端区
#区域处理程序
公共IActionResult OnGet(int?id)
{
if(id==null)
{
返回NotFound();
}
var celula=_context.Celulas
.包括(c=>c.检查表)
.然后包括(cl=>cl.Grupos)
.然后包括(cl=>cl.Items)
.其中(c=>c.Automated)
.FirstOrDefault(c=>c.Id==Id);
Celula=_mapper.Map(Celula);
if(Celula==null)
{
返回NotFound();
}
返回页();
}
public IActionResult OnPost()
{
返回重定向Topage(“../Index”);
}
公共类CelulaViewModel
{
公共int Id{get;set;}
公共字符串Nome{get;set;}
public int?CheckListId{get;set;}
公共CheckListViewModel检查表{get;set;}
}
公共类CheckListViewModel
{
公共int Id{get;set;}
公共字符串Nome{get;set;}
公共IList Grupos{get;set;}
公共IList Celulas{get;set;}
}
公共类CheckListGrupoViewModel
{
公共int Id{get;set;}
公共字符串Nome{get;set;}
公共IList项{get;set;}
}
公共类CheckListGrupoItemViewModel
{
公共int Id{get;set;}
公共字符串Nome{get;set;}
公共字符串值{get;set;}
公共int GrupoId{get;set;}
公共CheckListGrupoViewModel Grupo{get;set;}
}
目标是将收音机的值绑定到CheckBoxListGrupoItemViewModel的value属性
这些视图模型与实体完全相同,只是CheckBoxListGrupoItemViewModel有一个属性值来保存单选按钮值
我的问题是,我似乎无法让它绑定到它
注意:我还尝试将其绑定到字典,itemId作为键,value作为字符串
更新
使用For loops而不是foreach编辑UI代码,这样我可以在post后访问项目。您可以为每个收音机按钮设置值(例如Yes=1、No=2、None=3),并在razor页面中将其作为列表清单接收。例如 Page.cshtml
<form method="post">
<table class="table table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Yes</th>
<th>No</th>
<th>None</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < 4; i++)
{
<tr>
<th>Description @(i+1)</th>
<td><input type="radio" value="1" name="checklist[@i]" required /> </td>
<td><input type="radio" value="2" name="checklist[@i]" required/> </td>
<td><input type="radio" value="3" name="checklist[@i]" required/> </td>
</tr>
}
</tbody>
</table>
<input type="submit" value="submit"/>
</form>
标题
对
不
没有一个
@对于(int i=0;i<4;i++)
{
说明@(i+1)
}
页面模型:<
public class RadioModel : PageModel
{
[BindProperty]
public List<int> checklist { get; set; }
public void OnGet()
{
}
public void OnPost()
{
}
}
<table class="table table-bordered table-sm">
<thead>
<tr>
<th class="text-center">@Model.Celula.CheckList.Grupos[i].Nome</th>
<th class="text-center">Sim</th>
<th class="text-center">Não</th>
<th class="text-center">Não Aplicável</th>
</tr>
</thead>
<tbody>
@for (int j = 0; j < Model.Celula.CheckList.Grupos[i].Items.Count(); j++)
{
<tr>
<th class="pl-4">
<i class="fas fa-caret-right"></i>
<input type="hidden" asp-for="Celula.CheckList.Grupos[i].Items[j].Id" />
<span class="ml-3">@Model.Celula.CheckList.Grupos[i].Items[j].Nome</span>
</th>
<td class="text-center item">
<input type="radio" asp-for="Celula.CheckList.Grupos[i].Items[j].Value" value="S" required style="display:none;" />
<i class="fa fa-check" style="display:none;"></i>
</td>
<td class="text-center item">
<input type="radio" asp-for="Celula.CheckList.Grupos[i].Items[j].Value" value="N" required style="display:none;" />
<i class="fa fa-check" style="display:none;"></i>
</td>
<td class="text-center item">
<input type="radio" asp-for="Celula.CheckList.Grupos[i].Items[j].Value" value="NA" required style="display:none;" />
<i class="fa fa-check" style="display:none;"></i>
</td>
</tr>
}
</tbody>
</table>