Validation for TextArea using Razor MVC 4

1

I have the following modal and need to include validations so that the textarea is required when clicking the "Yes" button, that is, it can not be sent blank, and that textarea is at least 15 characters.

  @model TClient
  <div id="myModal" class="modal fade">
      <div class="modal-dialog">
          <div class="modal-content">
              @using (Html.BeginForm())
              {
                  <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                      <h4 class="modal-title">@(Model.IsLocked ? "Desbloquear" : "Bloquear") cliente</h4>
                  </div>
                  <div class="modal-body">
                      <p>Deseja realmente @(Model.IsLocked ? "desbloquear" : "bloquear") o cliente <strong>@Model.Name</strong>?</p>
                      <div class="row">
                          <div class="col-lg-12">
                              <hr />
                              <div class="form-group">
                                  <label class="control-label">
                                      Motivo:
                                  </label>
                                  <div>
                                      @this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)
                                  </div>
                              </div>
                          </div>
                      </div>
                  </div>
                  <div class="modal-footer">
                      <button class="btn" data-dismiss="modal" aria-hidden="true">
                          Não
                      </button>
                      <button type="submit" id="confirmar" class="btn btn-primary">
                          Sim
                      </button>
                  </div>
              }
          </div>
      </div>
  </div>
    
asked by anonymous 03.02.2017 / 19:27

1 answer

1

I can not see this approach well:

@model TClient

...

@this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)

The correct one would be to strongly type View :

@model MeuSistema.Models.MeuModel

And use the following cliché:

@Html.TextAreaFor(model => model.Motivo, new { @class = "form-control required", @cols = 50, @rows = 7 })
@Html.ValidationMessageFor(model => model.Motivo)

If you decorated the Model correctly:

[Required]
[DataType(DataType.MultilineText)]
public String Motivo { get; set; }

And jQuery.Validate is installed correctly, it should work without further configuration. Still, you can force form validation as follows (jQuery):

$('#botaoSubmit').on('click', function() {
    $("#form").valid();
});
    
16.02.2017 / 19:10