I don't want opinions. I want answers to this question below. I know within my Model classes, I can include Data Annotations to validate my forms:
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
[Display(Name = "Release Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode =true, DataFormatString = "{0:yyy-MM-dd}")]
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
However, there are already JavaScript libraries that validate the default behaviors (i.e. @Html.ValidationMessageFor()
):
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
And my question would be... Is it best practice to validate in the models only or within the views itself?? What is more safe in terms of maintaining security?
Please let me know if this is wrong question to ask and I will remove it quickly.
Thank you