【问题标题】:Adding the attribute Required to an input Text将属性Required添加到输入文本
【发布时间】:2013-11-18 08:37:47
【问题描述】:

我有一个 asp.net mvc4 应用程序,其中有:

 <li>
                @Html.Label("Login")
                @Html.TextBox("Login")

            </li>

我想像这样将属性 Required 添加到 TextBox login

<input type="Text name="login" Required /> 

我该怎么做?

【问题讨论】:

  • @Html.TextBox("Login", null, new { required = "required" }) 应该可以帮助您

标签: asp.net .net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

另一种方法是在模型中添加数据注释:

[Required(ErrorMessage = "Field  cannot be blank")]

【讨论】:

    【解决方案2】:

    试试这个

    @Html.TextBox(
        "Login", 
        null, 
        new { 
            required = "required" 
        }
    )
    

    【讨论】:

    • 但这会输入required = "required"。他只需要required。仪式?
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2021-08-29
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多