RAZOR中的表单输入语法

时间:2016-05-08 01:03:42

标签: asp.net-mvc-4 razor

我有这段代码

<div class="input-group">
    <input type="text" class="form-control" name="InputName" id="InputName" placeholder="Enter Name" required>
    <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div>

如何在razor中将相同的span类应用于下面的代码行?

@Html.TextBoxFor(m => m.Name,
                      new {
                            @class = "form-control",
                            @placeholder="Enter Name",
                            @requried="required"
                          }
                 )

帮助将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:2)

这应该做到

<div class="form-group">
@*label if you have any*@
  <div class="input-group">
    @Html.TextBoxFor(m => m.Name,new {@class = "form-control",@placeholder="EnterName",@requried="required"})
   <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
  </div>
 </div>

答案 1 :(得分:1)

试试这种方式

 <div class="input-group">
                              @Html.TextBoxFor(m => m.FirstName,
                              new
                              {
                                  @class = "form-control",
                                  @placeholder = "Enter Name",
                                  @requried = "required"
                              }

                         )
      <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk">  </span></span>
</div>

自定义html助手可能会提供解决方案。

检查How can I override the @Html.LabelFor template?

Render span tag with title attribute with ASP.NET MVC 3 Helpers