Here is my ENUM:
public enum ValidityTypes : int
{
[Description("Not Set")]
None = 0,
[Description("Zero Validity")]
ZeroValidity = 1,
[Description("Life Time Validity")]
LifeTimeValidity = 2,
[Description("N Months")]
NMonths = 3,
[Description("Rehire Validity in Months")]
RehireValidity = 4
}
Here I set the Model property:
public ValidityTypes ValidityType { get; set; }
Here is my View:
<label><b>Zero Validity:</b></label>
@if (Model.ValidityType == Constant.ValidityTypes.ZeroValidity)
{
@Html.RadioButtonFor(x => x.ValidityType, "1", new { @id = "validity1", style = "width:50px", @checked = "true" , onClick = "ValidityEnableDisable(this);" })
}
else
{
@Html.RadioButtonFor(x => x.ValidityType, "1", new { @id = "validity1", style = "width:50px", @checked = "false" , onClick = "ValidityEnableDisable(this);" })
}
<label><b>Life Time Validity: </b></label>
@if (Model.ValidityType == OneC.BGV.BE.Constant.ValidityTypes.LifeTimeValidity)
{
@Html.RadioButtonFor(x => x.ValidityType, "2", new { @id = "validity2", style = "width:50px", @checked = "true" , onClick = "ValidityEnableDisable(this);" })
}
else
{
@Html.RadioButtonFor(x => x.ValidityType, "2", new { @id = "validity2", style = "width:50px", @checked = "false" , onClick = "ValidityEnableDisable(this);" })
}
<label><b>'N' Months:</label>
@if (Model.ValidityType == OneC.BGV.BE.Constant.ValidityTypes.NMonths)
{
@Html.RadioButtonFor(x => x.ValidityType, "3", new { @id = "validity3", style = "width:50px", @checked = "true", onClick = "Validity3EnableDisable(this);" , autocomplete="off" })
@Html.TextBoxFor(x => x.ValidityValue, new { id = "NmonthTextbox", style = "width:50px" })
}
else
{
@Html.RadioButtonFor(x => x.ValidityType, "3", new { @id = "validity3", style = "width:50px", @checked = "false" , onClick = "Validity3EnableDisable(this);" , autocomplete="off"})
@Html.TextBoxFor(x => x.ValidityValue, new { id = "NmonthTextbox", style = "width:50px", disabled = "true" })
}
Now when I am loading this view I am getting model.Validitytype as ZeroValidity which is same as in the Database and satisfying the first if condition. But on complete view load the N months radio button is coming checked.
Why I am not able to show correct radio button selected?
Is it my @checked property of the control is not working?
checked="true"orchecked="false"orchecked="anythingAtAll"all mean the same thing - the radio button will be checked. Thecheckedattribute is abooleanattribute which means its the presence of it that determines if its checked. In your case the last one is always checked because that is the last one that is rendered (the attribute is removed from the previous ones because only one is allowed to be checked).You should never set the
checkedattribute because theRadioButtonFor()method does that based on the value of the model you binding to (that is how model binding works). Remove all yourif/elseblocks and change the code to simply beThen use css to style your elements rather than inline styles, e.g.
and use Unobtrusive JavaScript to handle your events