html - 是否可以在 html 中将单选按钮标签助手值属性设置为 "checked"?

标签 html asp.net-mvc radio-button asp.net-core-mvc tag-helpers

我正在寻找一种方法来使用单选按钮标签助手上的值属性来通知按钮是否被选中,而不是使用单独的字段进行选择。我找到了@Shyju 对 related question 的回答.我正在使用该答案的修改示例来说明我的意思。

假设我有一个 View 模型

public class ExampleViewModel
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public List<UserRole> Roles { get; set; }
}
public class UserRole
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    public bool IsSelected { get; set; }
}

在 Controller 中初始化

public IActionResult Example()
    {
        var vm = new ExampleViewModel
        {
            Roles = new List<UserRole>
            {
                new UserRole {Id = 1, RoleName = "Admin"},
                new UserRole {Id = 2, RoleName = "Editor"},
                new UserRole {Id = 3, RoleName = "Reader"}
            }
        };
        return View("Example",vm);
    }

并在 View 中使用

<form asp-controller="Example" asp-action="Save">
    <label class="label">User name</label>
    <div class="col-md-10">
        <input asp-for="UserName" type="text" />
    </div>
    <label class="label">Select a Role</label>
    <div class="col-md-10">
        <div class="btn-group" data-toggle="buttons">
            @for (var i = 0; i < Model.Roles.Count; i++)
            {
                <label class="btn btn-default">
                    <input asp-for="Roles[i].Id" type="radio" value="@Model.Roles[i].Id" />
                    <input asp-for="Roles[i].IsSelected" type="radio" value=true />
                    @Model.Roles[i].RoleName
                </label>
            }
        </div>
    </div>
    <input type="submit" />
</form>

我想用这个实现的是能够读取 Controller 的 post 方法中的 UserRole.IsSelected 字段,以查看是否选中了特定的 UserRole。如果用户只检查一个按钮并且不改变选择,则此解决方案有效。如果用户将选择更改为另一个,则用户触摸的所有 UserRole 值都将 IsSelected 字段设置为 true。

是否有可能以某种方式使用 HTML 设置 IsSelected 值以仅显示提交表单时选中的按钮,或者它是否需要 JavaScript 函数?

最佳答案

您的模型对于单选按钮(用于从多个按钮中选择一个)没有意义。如果你只想选择一个角色,那么你的模型需要是

public class ExampleViewModel
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public int SelectedRole { get; set; }
    public List<UserRole> RolesList { get; set; }
}
public class UserRole
{
    public int Id { get; set; }
    public string RoleName { get; set; }
}

在 View 中

@foreach(var role < Model.RolesList)
{
    <label class="btn btn-default">
        <input asp-for=SelectedRole" type="radio" value="role.Id" />
        <span>@role.RoleName<span>
    </label>
}

并且在 POST 方法中,SelectedRole 的值将包含所选 UserRoleId

或者,如果您想选择多个角色,则使用您现有的模型,但 View 变为

@for (var i = 0; i < Model.Roles.Count; i++)
{
    <label class="btn btn-default">
        <input asp-for="Roles[i].Id" type="hidden"/>
        <input asp-for="Roles[i].IsSelected" type="checkbox"/>
        <span>@Model.Roles[i].RoleName</span>
    </label>
}

在 POST 方法中,您可以使用

IEnumerable<UserRole> selectedRoles = model.Roles.Where(x => x.IsSelected);

关于html - 是否可以在 html 中将单选按钮标签助手值属性设置为 "checked"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38466900/

相关文章:

c# - SignalR:对方法名称感到困惑 - CamelCase 与 lowerCamelCase - 有什么意义?

javascript - AngularJS - 充当单选按钮的 3 按钮组

c# - 如何以编程方式触发单选按钮的事件处理程序?

html - Bootstrap 下拉菜单在移动设备上消失

html - 自定义 403 错误页面未加载 CSS

android - 在Android应用程序中居中对齐时如何找到 ImageView 的高度和宽度?

asp.net-mvc - 在 Windows Azure 上托管后,elmah 错误日志中充斥着 Controller 路径,未找到或未实现 IController 错误

javascript - 输入类型文件在 jquery ajax 中未序列化

jquery - 使用jquery更新onSelect()

php - 在 WhatsApp 消息中创建换行符