0

假设这是我的观点...

<div class="control-group">
         @Html.LabelFor(model => model.Title, new { @class = "control-label" })
         @Html.ValidationMessageFor(model => model.Title, null, new { @class = "help-inline" })
         <div class="controls">
            @Html.EditorFor(model => model.Title)

         </div>
      </div>

如果出现错误我们如何隐藏Label和显示validation message代替Label

请就此提出建议...

4

2 回答 2

0

Just to compliment Darin's answer, if you are using client-side validation you will need to do something like

$("#myForm").validate({
    invalidHandler: function(event, validator) {
        $('.control-label').hide();
    }
});

Otherwise the label won't hide.

It's worth still checking ModelState.IsValid when the page loads as well though incase the user has JS disabled.

于 2013-10-16T11:27:37.307 回答
0

只有在 ModelState 有效时才能显示标签:

<div class="control-group">
    @if (this.ViewData.ModelState.IsValid)
    {
        @Html.LabelFor(model => model.Title, new { @class = "control-label" })
    }
    @Html.ValidationMessageFor(model => model.Title, null, new { @class = "help-inline" })
    <div class="controls">
        @Html.EditorFor(model => model.Title)
    </div>
</div>

请注意,如果您使用客户端验证,您还需要连接到客户端上的 validate 方法并切换相应的标签可见性。

于 2013-10-16T11:14:18.603 回答