【发布时间】:2019-03-17 23:33:02
【问题描述】:
我不想要意见。我想在下面回答这个问题。 我知道在我的模型类中,我可以包含数据注释来验证我的表单:
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; }
}
但是,已经有 JavaScript 库可以验证默认行为(即@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" })
我的问题是......最佳实践是仅在模型中验证还是在视图本身中验证?在维护安全方面什么更安全?
如果这是一个错误的问题,请告诉我,我会尽快删除它。
谢谢
【问题讨论】:
-
模型验证在服务器端,JavaScript 验证在客户端。
标签: javascript c# asp.net-mvc validation