Validate controls in ASP.NET MVC views

1

What is most advisable to validate controls in the views (ASP.NET MVC) controls I refer to textbox, combobox, etc. In the textbox enter letters, numbers, text sizes that I think will be done by html, which would be recommended javascript, jquery?

  

Note : Validate on the client side.

    
asked by Pedro Ávila 27.12.2016 в 03:15
source

3 answers

2

If you use MVC , you can take advantage of MVC and create MODELS with DataAnnotations

using System.ComponentModel.DataAnnotations; 

So your classes would be:

public class Movie {
    public int ID { get; set; }

    [Required]
    public string Title { get; set; }

    [DataType(DataType.Date)]
    public DateTime ReleaseDate { get; set; }

    [Required]
    public string Genre { get; set; }

    [Range(1, 100)]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }

    [StringLength(5)]
    public string Rating { get; set; }
}

Like the previous example, and adding the DataAnnotations there is no need for you to validate or use something else in javascript , since when generating the view (based on a model with DataAnnotations ) the validations will be generated automatically .

The view would be something like that, based on the example of Microsoft:

    @model MvcMovie.Models.Movie

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Movie</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReleaseDate)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Genre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Genre)
            @Html.ValidationMessageFor(model => model.Genre)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>
        <div class="editor-label">
    @Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Rating)
    @Html.ValidationMessageFor(model => model.Rating)
</div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

You can see more information in the league: Adding Validation to the Model

    
answered by 27.12.2016 в 10:13
1

Friend if you are going to do it with jquery or javascript you do not need the validations of the MVC required. you can use jquery for the required fields and for field length validations. There are also functions already lists that validate you if it is numeric or text with regular expressions. you can visit jquery

    
answered by 28.12.2016 в 01:17
0

When validations can be done through HTML, I do them like that. And when there are not the ones I need to use Jquery and JqueryValidate. It is much easier than JavaScript, handles compatibility problems and allows many types of verification and issue error messages.

link

    
answered by 27.12.2016 в 04:05