Asp.net mvc 4 根据模型的有效状态有条件地在Razor中添加css类
我有相当标准的MVC代码Asp.net mvc 4 根据模型的有效状态有条件地在Razor中添加css类,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,我有相当标准的MVC代码 <div class="form-group"> @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-8 "> @Html.EditorFor(model => model.Descr
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8 ">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
@LabelFor(model=>model.Description,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Description,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Description,“,new{@class=“text danger”})
如果model.Description的验证失败,但我无法计算语法,我想在表单组中添加一个CSS类“has error”。我发现了很多有条件地添加文本的示例,但不基于模型的验证状态。类似的内容可以帮助您
<script type="text/javascript">
var isValid = @Html.Raw(Json.Encode(ViewData.ModelState.IsValid));
if (isValid != 'true') {
$('.form-selector').addClass('has-error');
}
</script>
var isValid=@Html.Raw(Json.Encode(ViewData.ModelState.isValid));
如果(isValid!=“true”){
$('.form selector').addClass('has-error');
}
如果无效,框架已经添加了一个class=“输入验证错误”
。那门课的风格就好了。