【问题标题】:TextArea field Validation in Asp.Net MVCAsp.Net MVC 中的 TextArea 字段验证
【发布时间】:2018-10-11 12:57:53
【问题描述】:

任何人都可以在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; }


【问题讨论】:

    标签: asp.net-mvc-4


    【解决方案1】:

    改变

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

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

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

    【讨论】:

      【解决方案2】:

      对于模型中的 TextAreaFor 验证,我已应用 "[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>
      

      输出

      【讨论】:

        猜你喜欢
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-21
        • 1970-01-01
        • 1970-01-01
        • 2020-02-27
        • 2012-09-13
        相关资源
        最近更新 更多