mvc。净核心单选按钮为每个和模型活页夹选择值

问题描述 投票:0回答:1

我有 4 个单选按钮,我想为每个单选按钮选择值并将它们绑定到模型绑定器,以便每个属性的值要么为 true 要么为 false。目前,我们需要单选按钮而不是复选框。我需要对它们进行分组,以便只选择单选按钮,其余按钮的值 = false。例如,如果我选择 Yes - Math - 单选按钮的值应该是 true,其余的应该是 false。

所以这是我的剃刀视图课程

<div class="radio-class">
                        <input class="radios__input" id="IsMath" name="SubjectRadio" type="radio" value="true">
                        <label class="label" for="IsMath">Yes - Math</label>
                    </div>
                    <div class="radios__item">
                        <input class="radios__input" id="IsPhysics" name="SubjectRadio" type="radio" value="true">
                        <label class="label" for="IsPhysics">Yes - Physics</label>
                    </div>
                    <div class="radios__item">
                        <input class="radios__input" id="IsChemistry" name="SubjectRadio" type="radio" value="true">
                        <label class="label" for="IsChemistry">Yes - Chemistry</label>
                    </div>
                    <div class="radios__item">
                        <input class="radios__input" id="IsNoSubjects" name="SubjectRadio" type="radio" value="true">
                        <label class="label" for="IsNoSubjects">No Subjects</label>
                    </div>

这是我的模型:

 [BindProperty]
    public bool? IsPhysics { get; set; } = false;

    [BindProperty]
    public bool? IsChemistry { get; set; } = false;

    [BindProperty]
    public bool? IsMath { get; set; } = false;

    [BindProperty]
    public bool? IsNoSubjects { get; set; } = false;

我从来没有得到它的个人价值。我希望我选择的其中一个的真值将是一个假值。谁能帮忙?我现在不想将代码更改为单选按钮列表,或者如果是那样的话。但是,在选择单选按钮后,我仍然需要将 true 和 false 传递给每个属性。

asp.net-core model-view-controller radio-button asp.net-core-3.1 model-binding
1个回答
0
投票

Model Binding 通过

name
属性而不是
id
属性来绑定属性。所以你需要像下面这样更改你的代码:

<div class="radio-class">
    <input class="radios__input" name="IsMath" id="SubjectRadio" type="radio" value="true">
    <label class="label" for="IsMath">Yes - Math</label>
</div>
<div class="radios__item">
    <input class="radios__input" name="IsPhysics" id="SubjectRadio" type="radio" value="true">
    <label class="label" for="IsPhysics">Yes - Physics</label>
</div>
<div class="radios__item">
    <input class="radios__input" name="IsChemistry" id="SubjectRadio" type="radio" value="true">
    <label class="label" for="IsChemistry">Yes - Chemistry</label>
</div>
<div class="radios__item">
    <input class="radios__input" name="IsNoSubjects" id="SubjectRadio" type="radio" value="true">
    <label class="label" for="IsNoSubjects">No Subjects</label>
</div>
            
<button type="submit">Submit</button> 

此外,不确定您的表单是否发送

post
get
请求,但如果您发送
get
请求,请务必将属性更改为
[BindProperty(SupportsGet = true)]


您可以关注的完整工作演示:

视图(Index.cshtml)

<form>
    <div class="radio-class">
        <input class="radios__input" name="IsMath" id="SubjectRadio" type="radio" value="true">
        <label class="label" for="IsMath">Yes - Math</label>
    </div>
    <div class="radios__item">
        <input class="radios__input" name="IsPhysics" id="SubjectRadio" type="radio" value="true">
        <label class="label" for="IsPhysics">Yes - Physics</label>
    </div>
    <div class="radios__item">
        <input class="radios__input" name="IsChemistry" id="SubjectRadio" type="radio" value="true">
        <label class="label" for="IsChemistry">Yes - Chemistry</label>
    </div>
    <div class="radios__item">
        <input class="radios__input" name="IsNoSubjects" id="SubjectRadio" type="radio" value="true">
        <label class="label" for="IsNoSubjects">No Subjects</label>
    </div>
                
    <button type="submit">Submit</button>       
</form>

控制器

public class HomeController : Controller
{
    public IActionResult Index()
    {            
        return View();        
    }
    [BindProperty(SupportsGet =true)]
    public bool? IsPhysics { get; set; } = false;

    [BindProperty(SupportsGet = true)]
    public bool? IsChemistry { get; set; } = false;

    [BindProperty(SupportsGet = true)]
    public bool? IsMath { get; set; } = false;

    [BindProperty(SupportsGet = true)]
    public bool? IsNoSubjects { get; set; } = false;
}

结果

© www.soinside.com 2019 - 2024. All rights reserved.