0

我在 MVC Razor 项目中使用 RadioButtonList 生成我的单选按钮。这是我的代码示例:

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

出于某种原因,我的单选按钮列表获得了预选值。第一个复选框总是被选中,这让我的 UI 有点混乱。

如何以一种好的方式禁用它?

一种方法是使用 Jquery 遍历整个页面并取消选择每个框。但这不是一个很好的解决恕我直言的工作。

编辑: 这是关于 HouseType 的更多信息,它是一个自定义枚举。

    public enum HouseType
{
    House,
    Apartment,
    Garage
};

并使用这条线调用它

public HouseType HouseType { get; set; }
4

1 回答 1

1

您可以HouseType在视图模型上将该属性设为可为空的类型。例如,如果它是枚举类型:

public HouseTypes? HouseType { get; set; }

或者如果它是一个整数:

public int? HouseType { get; set; }

更新:

看来您正在使用following helper. 此帮助程序不支持可为空的枚举值。所以适应它:

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());
    }
}

现在它将使用可为空的枚举,如果相应属性的值为空,它将不会选择任何单选按钮。

于 2012-10-16T07:34:57.740 回答