Asp.Net MVC中的TextArea字段验证

时间:2018-10-11 12:57:54

标签: asp.net-mvc-4

任何人都可以在asp.net mvc中建议文本区域字段验证吗。

我的文本查看区域:

@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; }


2 个答案:

答案 0 :(得分:0)

更改

@Html.ValidationMessagefor(m => m.EmailTemplate, new { @class = "text-danger"}); 

收件人

@Html.ValidationMessageFor(m => m.EmailTemplate, null, new { @class = "text-danger"})

由于@Html.ValidationMessageFor的第二个参数采用可选的验证消息(使用null或“”),而第三个参数采用htmlAttributes @class等。

答案 1 :(得分:0)

对于模型中的TextAreaFor Validation,我已应用“ [DataType(DataType.MultilineText)]” 属性以使其起作用。

模型

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>

输出

enter image description here