public static IHtmlString CheckBoxWithLabelFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression, string labelText, object htmlAttributes)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
object currentValue = metadata.Model;
string property = ExpressionHelper.GetExpressionText(expression);
var checkBox = new TagBuilder("input");
checkBox.AddCssClass("checkBoxWithLabel");
checkBox.GenerateId(property);
checkBox.Attributes["type"] = "checkbox";
checkBox.Attributes["name"] = property;
checkBox.Attributes["value"] = "true";
checkBox.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),false);/*added false*/
var hidden = new TagBuilder("input");
hidden.Attributes["type"] = "hidden";
hidden.Attributes["name"] = property;
hidden.Attributes["value"] = "false";
if (Equals(currentValue, true))
{
checkBox.Attributes["checked"] = "checked";
}
var label = new TagBuilder("label");
label.AddCssClass("checkBoxLabel");
var htmlText = label.ToString().Replace("</label>", "");
htmlText += checkBox.ToString(TagRenderMode.SelfClosing);
htmlText += hidden.ToString(TagRenderMode.SelfClosing);
htmlText += labelText + "</label>";
return new HtmlString(htmlText);
AnonymousObjectToHtmlAttributes (htmlAttributes) заменяет только «_» на «-». В то время как MergeAttributes ожидает тип ключа/значения и поэтому игнорирует существующие значения. Невозможно изменить/переместить объект HtmlAttributes в словарь с IEnumerable, IDictionary и т. Д. Я думаю, что MergeAttributes должен находиться в цикле для извлечения ключа/значений, но не уверен, что начинает катить мяч?Слияние атрибутов
Я хочу, чтобы класс имел исходное значение htmlAttributes «editableInNew editableInUpdate readonly» вместе с «checkBoxWithLabel», добавленным с .AddCssClass, но не могу заставить его работать, и я в тупике.
Да его время, чтобы пойти домой наверняка mecek ... '@ Html.CheckBoxWithLabelFor (х => x.Travel.MissedDepartureList.TPResponsibleYes, "Да", новый {@ = только для чтения "", @class = "editableInNew editableInUpdate readonly"}) ' Наведение на htmlAttributes дает –
Да, его время, чтобы вернуться домой наверняка mecek ... ' @ Html.CheckBoxWithLabelFor (x => x.Tra ..., "Yes", new { @ readonly = "", @class = "editableInNew editableInUpdate readonly"}) ' Наведение над htmlAttributes дает' {readonly = "", class = "editableInNew editableInUpdate readonly"} ' У меня есть стандартные флажки с атрибутами, переданными в этой работе и почти точно то же самое, что '@ Html.CheckBoxFor (x => x .ThirdParty.IsCompany, новый {@class = "editableInUpdate editableInNew readonly"}) ' –