Search code examples
asp.net-mvcasp.net-mvc-3razorradiobuttonlist

MVC Razor RadioButtonList without any preselected values


Im using RadioButtonList to generate my radio buttons, in a MVC Razor project. Here's an example of my code:

@Html.RadioButtonList(n => n.HouseType)

For some reason my radio button lists gets a preselected value. The first checkbox is always checked, which makes my UI kinda confusing.

How do I disable this in a good way?

One way is to loop through the whole page with Jquery and unselect each box. But thats not a pretty work around imho.

EDIT: Here's more info about HouseType, which is a custom enum.

    public enum HouseType
{
    House,
    Apartment,
    Garage
};

and its called upon by using this line

public HouseType HouseType { get; set; }

Solution

  • You could make the HouseType property a nullable type on your view model. For example if it is an enum type:

    public HouseTypes? HouseType { get; set; }
    

    or if it is an integer:

    public int? HouseType { get; set; }
    

    UPDATE:

    It seems that you are using the following helper. This helper doesn't support nullable enum values. So adapt it:

    public static class RaidioButtonListHelper
    {
        /// <summary>
        /// Create a radiobutton list from viewmodel.
        /// </summary>
        public static MvcHtmlString RadioButtonList<TModel, TResult>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, IEnumerable<SelectListItem> listOfValues = null)
        {
            var typeOfProperty = expression.ReturnType;
    
            // Added by Darin Dimitrov to support nullable enums
            var underlyingType = Nullable.GetUnderlyingType(typeOfProperty);
            if (underlyingType != null)
            {
                typeOfProperty = underlyingType;
            }
            // End of addition
    
            if (listOfValues == null && typeOfProperty.IsEnum)
            {
                listOfValues = new SelectList(Enum.GetValues(typeOfProperty));
            }
    
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    
            // Ctreat table
            TagBuilder tableTag = new TagBuilder("table");
            tableTag.AddCssClass("radio-main");
    
            // Create tr:s
            var trTagLable = new TagBuilder("tr id=\"" + metaData.PropertyName + "Lables\"");
            var trTagRadio = new TagBuilder("tr id=\"" + metaData.PropertyName + "Radios\"");
    
            foreach (SelectListItem item in listOfValues)
            {
                var text = item.Text;
                var value = item.Value ?? text;
    
                // Generate an id to be given to the radio button field 
                var id = string.Format("{0}_{1}", metaData.PropertyName, value);
    
                // Create the radiobuttons
                var radioTag = htmlHelper.RadioButtonFor(expression, value, new { id = id }).ToHtmlString();
    
                // Create the label for the radiobuttons.
                var labelTag = htmlHelper.Label(id, HttpUtility.HtmlEncode(text));
    
                // Add the lables and reaiobuttons to td:s
                var tdTagLable = new TagBuilder("td style=\"padding-left: 10px; text-align: center\"");
                var tdTagRadio = new TagBuilder("td style=\"padding-left: 10px; text-align: center\"");
                tdTagLable.InnerHtml = labelTag.ToString();
                tdTagRadio.InnerHtml = radioTag.ToString();
    
                // Add tds: to tr:s
                trTagLable.InnerHtml += tdTagLable.ToString();
                trTagRadio.InnerHtml += tdTagRadio.ToString();
    
            }
    
            // Add tr:s to table
            tableTag.InnerHtml = trTagLable.ToString() + trTagRadio.ToString();
    
            //Return the table tag
            return new MvcHtmlString(tableTag.ToString());
        }
    }
    

    Now it's gonna work with a nullable enum and it won't select any radio button if the value of the corresponding property is null.