您可以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());
}
}
现在它将使用可为空的枚举,如果相应属性的值为空,它将不会选择任何单选按钮。