I have a radiobuttonList which is binding data from Enum Class and its working correctly in the view.
But my concern is how can I set inital value of radiobutton to CROCount.ONE
.I have tried to set the initial value in the following way but couldnot get the desired result.
public enum CROCount
{
ONE = 1,
TWO = 2
}
ViewModel is
public class RegistraionVM
{
....
public EnumClass.CROCount CROCount { get; set; }
}
I generated the radio button list as follows.
<div>
@foreach (var count in Enum.GetValues(typeof(SMS.Models.EnumClass.CROCount)))
{
<label style="width:75px">
@Html.RadioButtonFor(m => m.RegistrationVenue, (int)count,
new { @class = "minimal single" })
@count.ToString()
</label>
}
</div>
Binding performed in the Controller is
public ActionResult Index(int walkInnId)
{
try
{
var _studentReg = new RegistraionVM
{
CROCount=EnumClass.CROCount.ONE
};
return View(_studentReg);
}
catch (Exception ex)
{
return View("Error");
}
}
Your binding your radio button to property CROCount
(not RegistrationVenue
) so your code should be
@Html.RadioButtonFor(m => m.CROCount, count, new { id = "", @class = "minimal single" })
Note that the 2nd parameter is count
(not (int)count
) so that you generate value="ONE"
and value="TWO"
. Note also the new { id = "",
removes the id
attribute which would otherwise result in duplicate id
attributes which is invalid html.