如何在Razor编辑视图中显示选中的单选按钮Asp Net Core MVC [英] How to show checked radio button in Razor edit view Asp net core mvc

查看:124
本文介绍了如何在Razor编辑视图中显示选中的单选按钮Asp Net Core MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在Razor视图中具有Asp净核心代码,

Despite of Asp net core code in Razor view:

@model List<SelectListItem>
<form asp-controller="Manage" asp-action="Change" method="post">
    @foreach (SelectListItem item in Model) {
        <br />
        <input asp-for="@item.Value" type="radio" value="@item.Value" /> @(item.Value + " - " + item.Text)
   }
   <br />
   <input type="submit" value="OK" />
</form>

在浏览器中选中所有单选按钮->查看源代码

all radio buttons are checked in browser -> view source

<input type="radio" value="1001" checked="checked" id="item_Value" name="item.Value" /> 1001 - Vilnius<br />
<input type="radio" value="1002" checked="checked" id="item_Value" name="item.Value" /> 1002 - Palanga<br />

如何检查所有单选按钮?

How it can be all radio buttons checked?

已添加:

var lst = new List<SelectListItem>() {
           new SelectListItem {
                Value = "1001",
                Text = "Vilnius",
                Selected = true
            },
            new SelectListItem {
                Value = "1002",
                Text = "Trakai",
                Selected = false
            }
        };

推荐答案

据我所知,设置了 checked ="checked" 属性是因为这些单选按钮的当前值等于模型值(在标签帮助程序的 asp-for 属性中分配的属性).因此,您当前使用 asp-for ="@ item.Value" 进行的设置在这里是错误的:

As far as I know, the checked="checked" attribute is set because the current value of those radio buttons are equal to model value (the property you're assigned in asp-for attribute inside tag helper). Hence, your current setup with asp-for="@item.Value" is wrong here:

<input asp-for="@item.Value" type="radio" value="@item.Value" />

如果要基于 SelectListItem 实例中的 Selected 属性设置其值,则需要将viewmodel类中的属性用于 asp-for 属性(该属性必须定义为值类型或字符串).

If you want to set their values based on Selected property inside SelectListItem instance, you need to use a property from your viewmodel class into asp-for attribute (the property must defined as value type or string).

以下是正确的示例设置:

Below is a proper example setup:

模型

// Viewmodel class
public class ViewModel
{
    // used to store selected value in select tag helper
    public string SelectedValue { get; set; }

    public List<SelectListItem> OptionList { get; set; }
}

控制器操作

// Populate select list
[HttpGet]
public IActionResult Change()
{
    var model = new ViewModel();
    model.OptionList = new List<SelectListItem>();

    // populate list values here
    model.OptionList.Add(new SelectListItem { Text = "1001", Value = "Vilnius", Selected = true });
    model.OptionList.Add(new SelectListItem { Text = "1002", Value = "Trakai", Selected = false });

    return View(model);
}

查看

@* Set viewmodel property into tag helper *@
@model ViewModel

<form asp-controller="Manage" asp-action="Change" method="post">
    @foreach (var item in Model.OptionList) 
    {
        <br />
        <input asp-for="SelectedValue" type="radio" value="@item.Value" /> @(item.Value + " - " + item.Text)
    }
    <br />
    <input type="submit" value="OK" />
</form>

相关问题:

单选按钮标记助手"会将每个单选按钮设置为选中状态

这篇关于如何在Razor编辑视图中显示选中的单选按钮Asp Net Core MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆