我在 asp.net core mvc 上有一個簡單的 Web 應用程式,其視圖具有復選框,我想將其更改為使用單選按鈕,但那里有一個 for 回圈,因此我無法使其作業。根據我的嘗試,或者單選按鈕全部被選中,或者沒有被選中,應該有一個特定的選擇
@model List<ChangeItemViewModel>
<form method="post">
<div class="card">
<div class="card-body">
@for (int i = 0; i < Model.Count; i )
{
<div class="form-check">
<input type="hidden" asp-for="@Model[i].Id" />
<input type="hidden" asp-for="@Model[i].Name" />
<input asp-for="@Model[i].Selected" class="form-check-input" />
<label asp-for="@Model[i].Selected" class="form-check-label">
@Model[i].Name
</label>
</div>
}
</div>
</div>
uj5u.com熱心網友回復:
您可以嘗試檢查 的值@Model[i].Selected
,并使用兩個單選按鈕替換一個復選框:
@model List<ChangeItemViewModel>
<form method="post">
<div class="card">
<div class="card-body">
@for (int i = 0; i < Model.Count; i )
{
<div class="form-check">
<input type="hidden" asp-for="@Model[i].Id" />
<input type="hidden" asp-for="@Model[i].Name" />
<label asp-for="@Model[i].Selected" class="form-check-label">
@Model[i].Name
</label>
@if (Model.list[i].Selected)
{
<input type="radio" value=true name="list[@i].Selected" checked><label>true</label>
<input type="radio" value=false name="list[@i].Selected"><label>false</label>
}
else
{
<input type="radio" value=true name="list[@i].Selected"><label>true</label>
<input type="radio" value=false name="list[@i].Selected" checked><label>false</label>
}
</div>
}
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/498271.html
標籤:html 网 asp.net-mvc asp.net 核心 模型视图控制器
上一篇:存盤更新、插入或洗掉陳述句影響了意外數量的行(0)。物體可能已被修改或洗掉
下一篇:在視圖中使用視圖模型時遇到問題