c# - How to add additional validation to Model field? -
i have second thing:
<td> @html.labelfor(m => m.number, titlehtmlattrs) </td> <td> <span class="element-value2"> @html.editorfor(m => m.number) @html.validationtooltipfor(m => m.number) </span> </td>
and how field looks in model:
[display(name = "special number")] [stringlength(20)] public string number { get; set; }
which means if wanted change field, can have value empty 20. it's ok, need additional validation. in model have fields:
public datetime? timeof { get; set; } public bool hastype { get; set; }
new validation should work only if timeof not null , hastype true. new validation should prevent empty values in number. basically, change (from empty 20) (from 1 20).
how correctly accomplish this?
p.s sorry bad english.
for complex validation logic, @ implementing ivalidatableobject
in viewmodel
, can place conditional validation logic inside validate
method. (caveat, server side)
public ienumerable<validationresult> validate(validationcontext validationcontext) { if (this.hastype) { // other conditional validation if (validationfails) { yield return new validationresult("descriptive error goes here"); } } // other validation here.
Comments
Post a Comment