【发布时间】:2019-09-07 12:52:10
【问题描述】:
在 .Net MVC 中。我有一个 html 控件。为了将它与模型属性绑定,我使用了 name 属性。我们如何将模型类属性中提供的验证(使用数据注释)获取到 html 控件中?
在 Cshtml 中
@using (Html.BeginForm("ClaimWarranty", "WarrentyClaim", FormMethod.Post, new{ enctype = "multipart/form-data" }))
{
<div class="form-group row">
<label for="" class="col-md-2 col-form-label input-label">Email Address:</label>
<div class="col-md-8">
<input type="text" name="Emailaddress" class="form-control input-style" placeholder="example@company.com">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" onclick="ValidateFileSize()" class="btn btn-default" />
</div>
</div>
}
//The model class is below;
public class ClaimWarranty
{
[Required(ErrorMessage = "Email ID is Required")]
[DataType(DataType.EmailAddress)]
[MaxLength(50)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Incorrect Email Format")]
public string Emailaddress { get; set; }
}
我正在使用 name 属性将文本框绑定到模型属性。
<input type="text" name="Emailaddress" class="form-control input-style" placeholder="example@company.com">
如何在不使用 jquery 验证或 razor 代码的情况下获得模型类中提供的 html 控件中的验证(使用数据注释)(使用数据注释)?
【问题讨论】:
-
在你的
<input>之后添加@Html.ValidationMessageFor(model =>model.Emailaddress , "", new { @class = "text-danger" }) -
感谢@Nijin Kooderi,上面的评论效果很好
-
不客气
标签: asp.net-mvc