MVC 3 data annotations turn off validation
I have Entity set and i create partial class
[Bind(Include = "ID,Note,Spec,Retired")]
[Meta开发者_运维百科dataType(typeof(SomeClass))]
public partial class Users
{
}
and in this class I have some data annotations
[Required(ErrorMessageResourceType = typeof(MyResources.Resources), ErrorMessageResourceName = "ReqField")]
[Display(Name = "Note")]
[StringLength(255, ErrorMessageResourceType = typeof(MyResources.Resources), ErrorMessageResourceName = "LenghtNotLonger255")]
public string Note
{
get;
set;
}
In View
<div class="editor-field">
@Html.EditorFor(model => model.Spec)
@Html.ValidationMessageFor(model => model.Spec)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Note)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.Note)
@Html.ValidationMessageFor(model => model.Note)
</div>
And everything works fine. But on edit I don't want to have validation on this Note field. How can I Do this.
The standard thing here is to do is to create a separate model (ViewModel) for that scenario without validation
精彩评论