Tôi đã một radiobuttonlistFor bộ chuyển đổi tùy chỉnh làm việc, nhưng nếu một người sử dụng tạo ra dữ liệu được thiết lập lại, và không có dữ liệu đã được đệ trình trước đây, một trong những nút radio (đầu tiên) luôn được chọn trước, tôi muốn tránh điều này, làm sao tôi có thể đạt được điều này?ASP.NET MVC RadioButtonListFor luôn cài sẵn
@Html.RadioButtonForSelectList(model => model.ViewModelForThingCreate.ThingTypeID, Model.ViewModelForCarCreate.CarTypeSelectList)
và:
public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(this HtmlHelper<TModel> HTMLHelper,Expression<Func<TModel, TProperty>> Expression, IEnumerable<SelectListItem> ListOfValues)
{
var MetaData = ModelMetadata.FromLambdaExpression(Expression, HTMLHelper.ViewData);
var SB = new StringBuilder();
if (ListOfValues != null)
{
foreach (SelectListItem Item in ListOfValues)
{
var ID = string.Format("{0}_{1}", MetaData.PropertyName, Item.Value);
var Radio = HTMLHelper.RadioButtonFor(Expression, Item.Value, new { id = ID }).ToHtmlString();
SB.AppendFormat("<label class=\"radio inline\" for=\"{0}\">{1} {2}</label>", ID, Radio, HttpUtility.HtmlEncode(Item.Text));
}
}
return MvcHtmlString.Create(SB.ToString());
}
Cảm ơn!
bạn cảm thấy thế nào khi sử dụng j? –
Đó là lúc nào cũng là phương sách cuối cùng ... – Baconbeastnz
check out này trơn nút radio helper từ Darin http://stackoverflow.com/questions/9553408/custom-helper-for-generating-html-tags-for-radio-button-and -associated-label –