当选中属性为false时,默认情况下选中单选按钮?

时间:2019-01-16 05:55:47

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我试图生成单选按钮,有条件地检查选中的属性,如果该值存在于数据库中,则应将其选中,否则选中的属性为false。因此最初数据库中没有行,并且所有单选按钮的checked属性也为false,但是仍然在UI上选中了它,请参见下图

enter image description here

所以不知道这是默认行为还是遗留的任何错误,下面是我的代码

ViewModel

#include <iostream> //Input/Output Library
#include <iomanip>
#include <string>
using namespace std;

//User Libraries

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...

//Function Prototypes

//Execution Begins Here!
int main(int argc, char** argv) {
    //Set the random number seed

    //Declare Variables
    char p1,p2;

    //Initialize or input i.e. set variable values
    cout<<"Rock Paper Scissors Game\n";
    cout<<"Input Player 1 Choice\n";
    cin>>p1;
    cout<<"Input Player 2 Choice\n";
    cin>>p2;
    //Map inputs -> outputs
    if (p1 == p2)
        cout<<"tie";
    else if ((tolower(p1) == 'p') && (tolower(p2) == 'r'))
        cout<<"Paper covers rock.";
    else if ((tolower(p2) == 'p') && (tolower(p1) == 'r'))
        cout<<"Paper covers rock.";

    //or combine above 2 statements to 
    //else if (((tolower(p1) == 'p') && (tolower(p2) == 'r')) || ((tolower(p2) == 'p') && (tolower(p1) == 'r')))
        //cout<<"Paper covers rock.";

    else if ((tolower(p1) == 's') && (tolower(p2) == 'r'))
        cout<<"Rock breaks scissors.";
    else if ((tolower(p2) == 's') && (tolower(p1) == 'r'))
        cout<<"Rock breaks scissors.";
    else if ((tolower(p1) == 's') && (tolower(p2) == 'p'))
        cout<<"Scissors cut paper.";
    else if ((tolower(p2) == 's') && (tolower(p1) == 'p'))
        cout<<"Scissors cut paper.";

    //Display the outputs

    //Exit stage right or left!
    return 0;
}

查看

public class CompanionProductVersion1ViewModel
{
    [Required(ErrorMessage = "Please select companion release - 1")]
    public string CompanionReleaseRadio1 { get; set; }
    public List<CompanionReleaseRadio1> CompanionReleaseRadios1 { get; set; }
}

public class CompanionReleaseRadio1
{
    public string RadioID { get; set; }
    public string RadioText { get; set; }
    public bool Checked { get; set; }
}

2 个答案:

答案 0 :(得分:3)

通过检查checked attribute behavior for radio button,我发现您的问题是由于checked属性是一个布尔属性,当该属性存在时,它指示相应的输入元素处于选中状态,而不论其值如何({{1 }}与checked="false"checked="true"相同)。

如果同一组中的多个单选按钮具有checked="checked"属性,则默认情况下,它将最后一个单选按钮设置为checked(根据您的原始代码,请参见this fiddle)。因此,当checked属性设置为false时,您需要利用checked内联函数帮助器来排除CompanionReleaseRadio1.Checked属性,如下例所示:

@functions

然后在@functions { private object SetRadioButtonChecked(bool isChecked, string RadioID) { // checked state from property if (isChecked) { // checked attribute is present return new { id = "CompanionReleaseRadio2" + RadioID, @checked = "checked" }; } else { // checked attribute is not present return new { id = "CompanionReleaseRadio2" + RadioID }; } } } 帮助器内部调用该内联函数,如下所示:

RadioButton

然后,所有@foreach (var companionReleases1 in Model.CompanionReleaseRadios1) { <div class="col-sm-4"> @Html.RadioButtonFor(model => Model.CompanionReleaseRadio1, companionReleases1.RadioID, @SetRadioButtonChecked(companionReleases1.Checked, companionReleases1.RadioID)) <span class="checkbox-label">@companionReleases1.RadioText</span> </div> } 属性设置为CompanionReleaseRadio1.Checked的单选按钮都应设置为未选中状态(有关预期结果,请参见this fiddle)。

参考:

Using @functions in an ASP.NET page with Razor Syntax

相关问题:

How to create a function in a cshtml template?

asp.net mvc3 checking radio button based on model

答案 1 :(得分:0)

我已经创建了Windows应用程序,并在Form1上添加了RadioButton。 并将单选按钮的选中属性设置为“ fasle”。但是,按照Windows应用程序的默认行为,它标记为选中了至少一个单选按钮。

一旦表单及其控件被加载,则只有我们可以将单选按钮标记为“ checked = false”。

在Windows应用程序中,Windows窗体有一个事件Shown()。因此,我们有代码在显示的事件上将checked = false标记为它,并且按预期方式工作。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        radioButton1.Checked = false;
    }
 }