有谁能在asp.net mvc.I中建议文本区域字段验证吗?我尝试了很多方法,但没有成功。
我对文本区域的看法:
@Html.TextAreaFor(m => m.EmailTemplate, new { rows = "10", cols = "150", @class = "form-control", @id = "Email", required = "required", @maxlength = "10000" })
@Html.ValidationMessagefor(m => m.EmailTemplate, new { @class = "text-danger"});
@Html.ValidationMessage("CustomError", new { @class = "text-danger" })我的模型:
[AllowHtml]
[Required(ErrorMessage ="Email Template is required")]
public string EmailTemplate { get; set; }发布于 2018-10-11 13:30:50
变化
@Html.ValidationMessagefor(m => m.EmailTemplate, new { @class = "text-danger"}); 至
@Html.ValidationMessageFor(m => m.EmailTemplate, null, new { @class = "text-danger"})由于@Html.ValidationMessageFor的第二个参数接受可选的验证消息(使用null或""),第三个参数接受htmlAttributes @class等。
发布于 2018-10-15 04:04:33
对于模型中的"DataType(DataType.MultilineText)"验证,我已经应用了TextAreaFor属性来使其工作。
模型
public class DemoModel
{
[Required(ErrorMessage = "Required")]
[AllowHtml]
[DataType(DataType.MultilineText)]
public string EmailTemplate { get; set; }
}视图
@model WebRedis.Models.DemoModel
@{
Layout = null;
}
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index1</title>
</head>
<body>
<div class="container">
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.EmailTemplate)
<br />
@Html.TextAreaFor(m => m.EmailTemplate, new { rows = "10", cols = "150", @class = "form-control", @maxlength = "10000" })
@Html.ValidationMessageFor(m => m.EmailTemplate, "", new { @class = "text-danger" })
</div>
<input id="Submit1" type="submit" value="submit" />
}
</div>
</body>
</html>输出

https://stackoverflow.com/questions/52760658
复制相似问题