当前位置: 移动技术网 > IT编程>开发语言>.net > MVC数据验证详解

MVC数据验证详解

2017年12月08日  | 移动技术网IT编程  | 我要评论

赫本 李春平,国际油价最新消息,翠青蛇

一、一般情况

对于使用过mvc框架的人来说,对mvc的数据验证不会陌生,比如,我有一个model如下:

public class userinfo
  {
    [required(errormessage = "username不可为空1111")]
    public string username { get; set; }
    public string sex { get; set; }
    public string mobile { get; set; }
    public string address { get; set; }
  }

前端:

@using (html.beginform()) 
{
  @html.antiforgerytoken()
  <div class="form-horizontal">
    <h4>userinfo</h4>
    <hr />
    @html.validationsummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
      @html.labelfor(model => model.username, htmlattributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.username, new { htmlattributes = new { @class = "form-control" } })
        @html.validationmessagefor(model => model.username, "", new { @class = "text-danger" })
      </div>
    </div>
    <div class="form-group">
      @html.labelfor(model => model.sex, htmlattributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.sex, new { htmlattributes = new { @class = "form-control" } })
        @html.validationmessagefor(model => model.sex, "", new { @class = "text-danger" })
      </div>
    </div>
    <div class="form-group">
      @html.labelfor(model => model.mobile, htmlattributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.mobile, new { htmlattributes = new { @class = "form-control" } })
        @html.validationmessagefor(model => model.mobile, "", new { @class = "text-danger" })
      </div>
    </div>
    <div class="form-group">
      @html.labelfor(model => model.address, htmlattributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.address, new { htmlattributes = new { @class = "form-control" } })
        @html.validationmessagefor(model => model.address, "", new { @class = "text-danger" })
      </div>
    </div>
    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="create" class="btn btn-default" />
      </div>
    </div>
  </div>
}

效果:

 

是的,mvc可以通过对一些属性添加一定的特性来对数据进行验证。这对大家来说可能并不陌生。

如果仅仅是这样就完事了,那么也就没事么意思了。

二、常用情况

在实际的开发中,我们大都是通过ef,或者其他方式,使得数据库中的每一个表或视图,都在代码中对应的一个类模型,对于通过数据库生成的模型,我们不宜修改,退一步讲,即使我们在这个类中对一些属性增加一些数据验证的特性,那么,数据库发生变化后,如果我再重新生成这些model,我们之前添加好的验证特性将没有了,那么,我们如何解决这样的问题呢?

假如:

public class userinfo
  {  
    public string username { get; set; }
    public string sex { get; set; }
    public string mobile { get; set; }
    public string address { get; set; }
  }

userinfo是通过数据库生成的一个模型,对于数据库生成的模型,我们不宜修改。但那是,我们又需要对这个模型中的某些属性进行数据验证,比如需要对username属性进行非空验证,那么我们如何做呢?

大家通常会想到部分类,是的,我们可以通过部分类来解决上述问题。

首先,我们将模型中的类加上关键字 partial ,然后我们再写一个这个模型的部分类。

public partial class userinfo
  {
    [required(errormessage = "username不可为空1111")]
    public string username { get; set; }
  }

但是,这样会提示我们一个错误,就是类中存在重复的属性,是的,部分类中,属性是不可以重名的。那么,我们该怎么办呢,mvc框架已经给了我们解决方案了。

我们可以这么写:

[metadatatype(typeof(meteuserinfo))]
  public partial class userinfo
  {
    private class meteuserinfo
    {
      [required(errormessage = "username不可为空1111")]
      public string username { get; set; }
    }
  }

这样,我们上述的问题就迎刃而解了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网