您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

总结MVC中数据验证实例

2025/4/26 3:09:16发布24次查看
一、一般情况
对于使用过mvc框架的人来说,对mvc的数据验证不会陌生,比如,我有一个model如下:
1     public class userinfo2     {3         [required(errormessage = username不可为空1111)]4         public string username { get; set; }5         public string sex { get; set; }6         public string mobile { get; set; }7         public string address { get; set; }8     }
前端:
 1 @using (html.beginform())   2 { 3     @html.antiforgerytoken() 4     <div class="form-horizontal"> 5         <h4>userinfo</h4> 6         <hr /> 7         @html.validationsummary(true, , new { @class = text-danger }) 8         <div class="form-group"> 9             @html.labelfor(model => model.username, htmlattributes: new { @class = control-label col-md-2 })10             <div class="col-md-10">11                 @html.editorfor(model => model.username, new { htmlattributes = new { @class = form-control } })12                 @html.validationmessagefor(model => model.username, , new { @class = text-danger })13             </div>14         </div>15         <div class="form-group">16             @html.labelfor(model => model.sex, htmlattributes: new { @class = control-label col-md-2 })17             <div class="col-md-10">18                 @html.editorfor(model => model.sex, new { htmlattributes = new { @class = form-control } })19                 @html.validationmessagefor(model => model.sex, , new { @class = text-danger })20             </div>21         </div>22         <div class="form-group">23             @html.labelfor(model => model.mobile, htmlattributes: new { @class = control-label col-md-2 })24             <div class="col-md-10">25                 @html.editorfor(model => model.mobile, new { htmlattributes = new { @class = form-control } })26                 @html.validationmessagefor(model => model.mobile, , new { @class = text-danger })27             </div>28         </div>29         <div class="form-group">30             @html.labelfor(model => model.address, htmlattributes: new { @class = control-label col-md-2 })31             <div class="col-md-10">32                 @html.editorfor(model => model.address, new { htmlattributes = new { @class = form-control } })33                 @html.validationmessagefor(model => model.address, , new { @class = text-danger })34             </div>35         </div>36         <div class="form-group">37             <div class="col-md-offset-2 col-md-10">38                 <input type="submit" value="create" class="btn btn-default" />39             </div>40         </div>41     </div>42 }
效果:
是的,mvc可以通过对一些属性添加一定的特性来对数据进行验证。这对大家来说可能并不陌生。
如果仅仅是这样就完事了,那么也就没事么意思了。
二、常用情况
在实际的开发中,我们大都是通过ef,或者其他方式,使得数据库中的每一个表或视图,都在代码中对应的一个类模型,对于通过数据库生成的模型,我们不宜修改,退一步讲,即使我们在这个类中对一些属性增加一些数据验证的特性,那么,数据库发生变化后,如果我再重新生成这些model,我们之前添加好的验证特性将没有了,那么,我们如何解决这样的问题呢?
假如:
1     public class userinfo2     {     3         public string username { get; set; }4         public string sex { get; set; }5         public string mobile { get; set; }6         public string address { get; set; }7     }
userinfo是通过数据库生成的一个模型,对于数据库生成的模型,我们不宜修改。但那是,我们又需要对这个模型中的某些属性进行数据验证,比如需要对username属性进行非空验证,那么我们如何做呢?
大家通常会想到部分类,是的,我们可以通过部分类来解决上述问题。
首先,我们将模型中的类加上关键字 partial ,然后我们再写一个这个模型的部分类。
1     public partial class userinfo2     {3         [required(errormessage = username不可为空1111)]4         public string username { get; set; }5     }
但是,这样会提示我们一个错误,就是类中存在重复的属性,是的,部分类中,属性是不可以重名的。那么,我们该怎么办呢,mvc框架已经给了我们解决方案了。
我们可以这么写:
1     [metadatatype(typeof(meteuserinfo))]2     public partial class userinfo3     {4         private class  meteuserinfo5         {6             [required(errormessage = username不可为空1111)]7             public string username { get; set; }8         }9     }
这样,我们上述的问题就迎刃而解了。
以上就是总结mvc中数据验证实例的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product