如何添加属性值不 [英] How to add attribute without value

查看:148
本文介绍了如何添加属性值不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过HTML帮助和TagBuilder生成HTML文本框。

我们有方法TagBuilder.Attributes.Add(键,值)

但对于HTML5必需的属性不需要值传递的,所以如果我通过空字符串,然后输出所需=值

让我怎么没有传递值添加所需的属性?

 公共静态IHtmlString AppTextBox(此的HtmlHelper帮手,弦模型)
    {
        VAR输入=新TagBuilder(输入);
        input.Attributes.Add(类,表单控制);
        input.Attributes.Add(NG-模式,模式);        input.Attributes.Add(必需的,);        返回新MvcHtmlString(input.ToString(TagRenderMode.Normal));
    }


解决方案

这也是有效传递属性作为值的名称:

  input.Attributes.Add(需要,要求);

I am generating HTML textbox through the html helper and TagBuilder.

we have the method TagBuilder.Attributes.Add("key","value")

but for HTML5 required attribute does not need value to be passed, so if i pass empty string then the output with value of required = ""

So how do i add required attribute without passing the value?

 public static IHtmlString AppTextBox(this HtmlHelper helper, string model)
    {
        var input = new TagBuilder("input");
        input.Attributes.Add("class", "form-control");
        input.Attributes.Add("ng-model", model);

        input.Attributes.Add("required","");

        return new MvcHtmlString(input.ToString(TagRenderMode.Normal));
    }

解决方案

It's also valid to pass the name of the attribute as the value:

input.Attributes.Add("required", "required");

这篇关于如何添加属性值不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆