首页后端开发ASP.NET对MVC进行数据验证详解

对MVC进行数据验证详解

时间2024-01-30 16:07:03发布访客分类ASP.NET浏览606
导读:收集整理的这篇文章主要介绍了对MVC进行数据验证详解,觉得挺不错的,现在分享给大家,也给大家做个参考。这篇文章主要为大家详细介绍了MVC数据验证的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下一、一般情况对于使用过MVC框架的人...
收集整理的这篇文章主要介绍了对MVC进行数据验证详解,觉得挺不错的,现在分享给大家,也给大家做个参考。这篇文章主要为大家详细介绍了MVC数据验证的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、一般情况

对于使用过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()  p class="form-horizontal">
        h4>
    UserInfo/h4>
        hr />
    @Html.ValidationSummary(true, "", new {
 @class = "text-danger" }
    )    p class="form-group">
          @Html.LabelFor(model =>
 model.UserName, htmlAttributes: new {
 @class = "control-label col-md-2" }
    )      p class="col-md-10">
            @Html.EdITorFor(model =>
 model.UserName, new {
 htmlAttributes = new {
 @class = "form-control" }
 }
    )        @Html.ValidationMessageFor(model =>
 model.UserName, "", new {
 @class = "text-danger" }
    )      /p>
        /p>
        p class="form-group">
          @Html.LabelFor(model =>
 model.Sex, htmlAttributes: new {
 @class = "control-label col-md-2" }
    )      p class="col-md-10">
            @Html.EditorFor(model =>
 model.Sex, new {
 htmlAttributes = new {
 @class = "form-control" }
 }
    )        @Html.ValidationMessageFor(model =>
 model.Sex, "", new {
 @class = "text-danger" }
    )      /p>
        /p>
        p class="form-group">
          @Html.LabelFor(model =>
 model.Mobile, htmlAttributes: new {
 @class = "control-label col-md-2" }
    )      p class="col-md-10">
            @Html.EditorFor(model =>
 model.Mobile, new {
 htmlAttributes = new {
 @class = "form-control" }
 }
    )        @Html.ValidationMessageFor(model =>
 model.Mobile, "", new {
 @class = "text-danger" }
    )      /p>
        /p>
        p class="form-group">
          @Html.LabelFor(model =>
 model.Address, htmlAttributes: new {
 @class = "control-label col-md-2" }
    )      p class="col-md-10">
            @Html.EditorFor(model =>
 model.Address, new {
 htmlAttributes = new {
 @class = "form-control" }
 }
    )        @Html.ValidationMessageFor(model =>
 model.Address, "", new {
 @class = "text-danger" }
    )      /p>
        /p>
        p class="form-group">
          p class="col-md-offset-2 col-md-10">
            input tyPE="submit" value="Create" class="BTn btn-default" />
          /p>
        /p>
      /p>
}
    

效果:

是的,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;
 }
    }
  }
    

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

以上就是对MVC进行数据验证详解的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

详解验证

若转载请注明出处: 对MVC进行数据验证详解
本文地址: https://pptw.com/jishu/592739.html
javascript怎么新建一个对象 C#如何实现自动更新本地程序的实例分析

游客 回复需填写必要信息