c# - Does the DataTypeAttribute on a model do validation in MVC 3? -
the default asp.net mvc 3 internet application template includes following model:
public class registermodel { [required] [display(name = "user name")] public string username { get; set; } [required] [datatype(datatype.emailaddress)] [display(name = "email address")] public string email { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 6)] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [datatype(datatype.password)] [display(name = "confirm password")] [compare("password", errormessage = "the password , confirmation password not match.")] public string confirmpassword { get; set; } }
in account/register action asking email address, seems can type in field , accept it.
does datatype(datatype.emailaddress) trigger validation? seems not. if doesn't validate type, purpose?
so far i'm aware, datatype attribute used formatting when use @html.editorfor(model => model.field)
.
examples model fields, cshtml , resulting html:
model fields:
[required] [datatype(datatype.text)] [display(name = "name")] public string name { get; set; } [required] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [required] [datatype(datatype.multilinetext)] [display(name = "description")] public string desc { get; set; }
cshtml:
<div class="form-section"> @html.labelfor(x => x.name) @html.editorfor(x => x.name) </div> <div class="form-section"> @html.labelfor(x => x.password) @html.editorfor(x => x.password) </div> <div class="form-section"> @html.labelfor(x => x.desc) @html.editorfor(x => x.desc) </div>
resulting html:
<div class="form-section"> <label for="name">name</label> <input class="text-box single-line" id="name" name="name" type="text" value=""> </div> <div class="form-section"> <label for="password">password</label> <input class="text-box single-line password" id="password" name="password" type="password" value=""> </div> <div class="form-section"> <label for="desc">description</label> <textarea class="text-box multi-line" id="desc" name="desc"></textarea> </div>
i know it's old post, maybe can shine light on situation others come way
edit:
there may validation attached these client side. not rely on these attributes actual validation, should done server side (client side user experience)
Comments
Post a Comment