本文整理汇总了C#中TagBuilder.MergeAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# TagBuilder.MergeAttributes方法的具体用法?C# TagBuilder.MergeAttributes怎么用?C# TagBuilder.MergeAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TagBuilder
的用法示例。
在下文中一共展示了TagBuilder.MergeAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImageActionLink
public static MvcHtmlString ImageActionLink(
this HtmlHelper helper,
string imageUrl,
string altText,
string actionName,
string controllerName,
object routeValues,
object linkHtmlAttributes,
object imgHtmlAttributes)
{
var linkAttributes = AnonymousObjectToKeyValue(linkHtmlAttributes);
var imgAttributes = AnonymousObjectToKeyValue(imgHtmlAttributes);
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute("src", imageUrl);
imgBuilder.MergeAttribute("alt", altText);
imgBuilder.MergeAttributes(imgAttributes, true);
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
var linkBuilder = new TagBuilder("a");
linkBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
linkBuilder.MergeAttributes(linkAttributes, true);
var text = linkBuilder.ToString(TagRenderMode.StartTag);
text += imgBuilder.ToString(TagRenderMode.SelfClosing);
text += linkBuilder.ToString(TagRenderMode.EndTag);
return MvcHtmlString.Create(text);
}
示例2: ActionLinkWithGlyphIcon
// example usage:
// <li>@Html.ActionLinkWithGlyphIcon(Url.Action("Index"),
// "Back to List",
// "icon-list")</li>
// instead of "icon-list", we could also use GlyphIcons.list
public static MvcHtmlString ActionLinkWithGlyphIcon(this HtmlHelper helper,
string action,
string text,
string glyphs,
string tooltip = "",
IDictionary<string, object> htmlAttributes = null)
{
var glyph = new TagBuilder("i");
glyph.MergeAttribute("class", glyphs);
var anchor = new TagBuilder("a");
anchor.MergeAttribute("href", action);
if(!string.IsNullOrEmpty(tooltip))
anchor.MergeAttributes(
new Dictionary<string, object>()
{
{ "rel", "tooltip" },
{ "data-placement", "top" },
{ "title", tooltip }
}
);
if(htmlAttributes != null)
anchor.MergeAttributes(htmlAttributes, true);
anchor.InnerHtml = glyph + " " + text;
return MvcHtmlString.Create(anchor.ToString());
}
示例3: BuildCheckBox
private IHtmlString BuildCheckBox(string name, bool? isChecked, IDictionary<string, object> attributes)
{
TagBuilder builder = new TagBuilder("input");
builder.MergeAttribute("type", "checkbox", replaceExisting: true);
builder.GenerateId(name);
builder.MergeAttributes(attributes, replaceExisting: true);
builder.MergeAttribute("name", name, replaceExisting: true);
if (UnobtrusiveJavaScriptEnabled)
{
var validationAttributes = _validationHelper.GetUnobtrusiveValidationAttributes(name);
builder.MergeAttributes(validationAttributes, replaceExisting: false);
}
var model = ModelState[name];
if (model != null && model.Value != null)
{
bool modelValue = (bool)ConvertTo(model.Value, typeof(bool));
isChecked = isChecked ?? modelValue;
}
if (isChecked.HasValue)
{
if (isChecked.Value == true)
{
builder.MergeAttribute("checked", "checked", replaceExisting: true);
}
else
{
builder.Attributes.Remove("checked");
}
}
AddErrorClass(builder, name);
return builder.ToHtmlString(TagRenderMode.SelfClosing);
}
示例4: TextAreaHelper
private static string TextAreaHelper(this HtmlHelper htmlHelper, string name, bool useViewData, string value, IDictionary<string, object> implicitAttributes, IDictionary<string, object> explicitParameters, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(name)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
}
TagBuilder tagBuilder = new TagBuilder("textarea");
// Add implicit attributes.
tagBuilder.MergeAttributes(implicitAttributes);
tagBuilder.GenerateId(name);
// Merge htmlAttributes.
tagBuilder.MergeAttributes(htmlAttributes, true);
// Override all the attributes with explicit parameters.
tagBuilder.MergeAttributes(explicitParameters, true);
tagBuilder.MergeAttribute("name", name, true);
// If there are any errors for a named field, we add the css attribute.
ModelState modelState;
if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState)) {
if (modelState.Errors.Count > 0) {
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
}
// The first newline is always trimmed when a TextArea is rendered, so we add an extra one
// in case the value being rendered is something like "\r\nHello".
// The attempted value receives precedence over the explicitly supplied value parameter.
string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));
tagBuilder.SetInnerText(Environment.NewLine + (attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : value)));
return tagBuilder.ToString(TagRenderMode.Normal);
}
示例5: ImageActionLink
public static MvcHtmlString ImageActionLink(this HtmlHelper helper , string imagePath , string actionName , string controllerName , object routeValues , object imageAttributes , object linkAttributes)
{
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext , helper.RouteCollection);
var aElementBuilder = new TagBuilder("a"); aElementBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes));
aElementBuilder.MergeAttribute("href" , urlHelper.Action(actionName , controllerName , routeValues));
var imgElementBuilder = new TagBuilder("img"); aElementBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes));
imgElementBuilder.MergeAttribute("src" , imagePath); aElementBuilder.InnerHtml = imgElementBuilder.ToString(TagRenderMode.SelfClosing);
return new MvcHtmlString(aElementBuilder.ToString(TagRenderMode.Normal));
}
示例6: BeginActionLink
public static MvcAnchor BeginActionLink(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, RouteValueDictionary htmlAttributes)
{
var targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true);
var builder = new TagBuilder("a");
builder.MergeAttributes(htmlAttributes);
builder.MergeAttribute("href", targetUrl);
builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
return new MvcAnchor(ajaxHelper.ViewContext);
}
示例7: Textbox
/// <summary>
/// Textboxes the specified ajax helper.
/// @Ajax.Textbox("search",
///new AjaxOptions {Url = @Url.Action("GetTime") ,UpdateTargetId = "divTime",InsertionMode = InsertionMode.Replace
///}, new { size = 50 })
/// </summary>
/// <param name="ajaxHelper">The ajax helper.</param>
/// <param name="name">The name.</param>
/// <param name="ajaxOptions">The ajax options.</param>
/// <param name="htmlAttributes">The HTML attributes.</param>
/// <returns></returns>
public static MvcHtmlString Textbox(this AjaxHelper ajaxHelper, string name,
AjaxOptions ajaxOptions, object htmlAttributes)
{
var tag = new TagBuilder("input");
tag.MergeAttribute("name", name);
tag.MergeAttribute("type", "text");
tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
tag.MergeAttributes((ajaxOptions ?? new AjaxOptions()).ToUnobtrusiveHtmlAttributes());
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
示例8: ToHtmlString
public override string ToHtmlString()
{
VerifySettings();
string fullName = Name;
var inputItem = EnumItem<InputType>.Create(InputType);
string inputType = inputItem.Description.ToLowerInvariant();
var tagBuilder = new TagBuilder("input");
tagBuilder.MergeAttributes(HtmlAttributes);
tagBuilder.MergeAttribute("type", inputType);
tagBuilder.MergeAttribute("name", fullName, true);
tagBuilder.GenerateId(fullName);
object value = Value ?? string.Empty;
string valueParameter = HtmlHelper.FormatValue(value, Format);
tagBuilder.MergeAttribute("value", valueParameter, true);
// if explicitly checked or model data = value
if (Checked || (Metadata != null && Metadata.Model == Value))
tagBuilder.MergeAttribute("checked", "checked");
foreach (string cssClass in CssClasses)
tagBuilder.AddCssClass(cssClass);
// If there are any errors for a named field, we add the css attribute.
ModelState modelState;
if (HtmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState) && modelState.Errors.Count > 0)
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
tagBuilder.MergeAttributes(HtmlHelper.GetUnobtrusiveValidationAttributes(Name, Metadata));
if (!CanWrite())
{
tagBuilder.MergeAttribute("disabled", "disabled", true);
if (DeniedClass.HasValue())
tagBuilder.AddCssClass(DeniedClass);
}
else if (GrantedClass.HasValue())
{
tagBuilder.AddCssClass(GrantedClass);
}
return tagBuilder.ToString(TagRenderMode.SelfClosing);
}
示例9: ImageActionLink
public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imgSrc, string altText, string text, string action, string controller, object routeValues, object htmlAttributes)
{
var url = UrlHelper.GenerateUrl(null, action, controller, new RouteValueDictionary(routeValues), helper.RouteCollection, helper.ViewContext.RequestContext, true);
var tbImg = new TagBuilder("img");
tbImg.MergeAttribute("src", imgSrc);
tbImg.MergeAttribute("alt", altText);
tbImg.MergeAttribute("border", "0");
tbImg.MergeAttribute("title", altText);
var imgString = tbImg.ToString(TagRenderMode.SelfClosing);
var tbA = new TagBuilder("a")
{
InnerHtml = imgString
};
if (!string.IsNullOrEmpty(text))
{
tbA.InnerHtml += HttpUtility.HtmlEncode(text);
}
tbA.MergeAttribute("href", url);
if (htmlAttributes != null)
{
IDictionary<string, object> additionAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
tbA.MergeAttributes(additionAttributes);
}
return MvcHtmlString.Create(tbA.ToString());
}
示例10: Button
public static TagBuilder Button(string name, string buttonText, HtmlButtonType type, string onClickMethod, IDictionary<string, object> htmlAttributes)
{
if (name == null)
{
throw new ArgumentNullException("name");
}
TagBuilder buttonTag = new TagBuilder("button");
if (!String.IsNullOrEmpty(name))
{
buttonTag.MergeAttribute("name", name);
}
buttonTag.MergeAttribute("type", type.ToString().ToLowerInvariant());
buttonTag.InnerHtml = buttonText;
if (!String.IsNullOrEmpty(onClickMethod))
{
buttonTag.MergeAttribute("onclick", onClickMethod);
}
buttonTag.MergeAttributes(htmlAttributes, true);
return buttonTag;
}
示例11: ValidationSummaryJQuery
public static MvcHtmlString ValidationSummaryJQuery(this HtmlHelper htmlHelper, string message,
IDictionary<string, object> htmlAttributes)
{
if (!htmlHelper.ViewData.ModelState.IsValid)
return htmlHelper.ValidationSummary(message, htmlAttributes);
// use default Validation Summary rendering if ModelState is invalid
var sb = new StringBuilder(Environment.NewLine);
var divBuilder = new TagBuilder("div");
divBuilder.MergeAttributes(htmlAttributes);
divBuilder.AddCssClass(HtmlHelper.ValidationSummaryValidCssClassName); // intentionally add VALID css class
if (!string.IsNullOrEmpty(message))
{
//--------------------------------------------------------------------------------
// Build an EMPTY error summary message <span style="display:none"> tag
//--------------------------------------------------------------------------------
var spanBuilder = new TagBuilder("span");
//spanBuilder.MergeAttribute("style", "display:none");
spanBuilder.SetInnerText(message);
sb.Append(spanBuilder.ToString(TagRenderMode.Normal)).Append(Environment.NewLine);
}
divBuilder.InnerHtml = sb.ToString();
return MvcHtmlString.Create(divBuilder.ToString(TagRenderMode.Normal));
}
示例12: ProcessAsync
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (_bundleManager.Exists(Source))
{
var result = (await _smidgeHelper.GenerateJsUrlsAsync(Source, Debug)).ToArray();
var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value);
using (var writer = new StringWriter())
{
foreach (var s in result)
{
var builder = new TagBuilder(output.TagName)
{
TagRenderMode = TagRenderMode.Normal
};
builder.MergeAttributes(currAttr);
builder.Attributes["src"] = s;
builder.WriteTo(writer, _encoder);
}
writer.Flush();
output.PostElement.SetContent(new HtmlString(writer.ToString()));
}
//This ensures the original tag is not written.
output.TagName = null;
}
else
{
//use what is there
output.Attributes["src"] = Source;
}
}
示例13: EmailLink
public static IHtmlString EmailLink( this HtmlHelper html, string text, string emailAddress, string subject = null, string body = null )
{
// Input validation.
if( String.IsNullOrEmpty( text ) || String.IsNullOrEmpty( emailAddress ) )
{
return null;
}
// Encode the sensitive data.
string encodedEmailAddress = BitConverter.ToString( Encoding.ASCII.GetBytes( emailAddress ) ).Replace( "-", String.Empty );
string encodedText = BitConverter.ToString( Encoding.ASCII.GetBytes( text ) ).Replace( "-", String.Empty );
TagBuilder tag = new TagBuilder( "a" );
tag.AddCssClass( "email-link" );
tag.MergeAttribute( "href", "#" );
tag.MergeAttributes( new Dictionary<string, string>
{
{ "href", "#" },
{ "data-code", encodedEmailAddress },
{ "data-text", encodedText },
{ "data-subject", subject },
{ "data-body", body }
} );
return new HtmlString( tag.ToString( TagRenderMode.Normal ) );
}
示例14: DeleteLink
public static MvcHtmlString DeleteLink(
this HtmlHelper helper,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes)
{
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.Action(actionName, controllerName, routeValues);
var form = new TagBuilder("form");
form.Attributes.Add("action", url);
form.Attributes.Add("method", "post");
form.Attributes.Add("style", "display:inline");
var formControls = helper.HttpMethodOverride(HttpVerbs.Delete).ToString();
var submit = new TagBuilder("input");
submit.Attributes.Add("type", "Submit");
submit.Attributes.Add("value", "Delete");
if (htmlAttributes != null)
{
submit.MergeAttributes(new RouteValueDictionary(htmlAttributes));
}
formControls += submit.ToString();
form.InnerHtml = formControls;
return MvcHtmlString.Create(form.ToString());
}
示例15: BuildControl
protected virtual void BuildControl(TagBuilder builder, CustomFieldDefinition fieldDefinition, string fieldValue, object htmlAttributes, ViewContext viewContext)
{
if (htmlAttributes != null)
{
builder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), true);
}
}