一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - MVC數(shù)據(jù)驗(yàn)證詳解

MVC數(shù)據(jù)驗(yàn)證詳解

2020-05-07 13:37萌萌丶小魔王 ASP.NET教程

這篇文章主要為大家詳細(xì)介紹了MVC數(shù)據(jù)驗(yàn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、一般情況

對(duì)于使用過(guò)MVC框架的人來(lái)說(shuō),對(duì)MVC的數(shù)據(jù)驗(yàn)證不會(huì)陌生,比如,我有一個(gè)Model如下:

?
1
2
3
4
5
6
7
8
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; }
  }

前端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@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數(shù)據(jù)驗(yàn)證詳解

是的,MVC可以通過(guò)對(duì)一些屬性添加一定的特性來(lái)對(duì)數(shù)據(jù)進(jìn)行驗(yàn)證。這對(duì)大家來(lái)說(shuō)可能并不陌生。

如果僅僅是這樣就完事了,那么也就沒(méi)事么意思了。

二、常用情況

在實(shí)際的開(kāi)發(fā)中,我們大都是通過(guò)EF,或者其他方式,使得數(shù)據(jù)庫(kù)中的每一個(gè)表或視圖,都在代碼中對(duì)應(yīng)的一個(gè)類(lèi)模型,對(duì)于通過(guò)數(shù)據(jù)庫(kù)生成的模型,我們不宜修改,退一步講,即使我們?cè)谶@個(gè)類(lèi)中對(duì)一些屬性增加一些數(shù)據(jù)驗(yàn)證的特性,那么,數(shù)據(jù)庫(kù)發(fā)生變化后,如果我再重新生成這些Model,我們之前添加好的驗(yàn)證特性將沒(méi)有了,那么,我們?nèi)绾谓鉀Q這樣的問(wèn)題呢?

假如:

?
1
2
3
4
5
6
7
public class UserInfo
  
    public string UserName { get; set; }
    public string Sex { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
  }

UserInfo是通過(guò)數(shù)據(jù)庫(kù)生成的一個(gè)模型,對(duì)于數(shù)據(jù)庫(kù)生成的模型,我們不宜修改。但那是,我們又需要對(duì)這個(gè)模型中的某些屬性進(jìn)行數(shù)據(jù)驗(yàn)證,比如需要對(duì)UserName屬性進(jìn)行非空驗(yàn)證,那么我們?nèi)绾巫瞿兀?/p>

大家通常會(huì)想到部分類(lèi),是的,我們可以通過(guò)部分類(lèi)來(lái)解決上述問(wèn)題。

首先,我們將模型中的類(lèi)加上關(guān)鍵字 partial ,然后我們?cè)賹?xiě)一個(gè)這個(gè)模型的部分類(lèi)。

?
1
2
3
4
5
public partial class UserInfo
  {
    [Required(ErrorMessage = "UserName不可為空1111")]
    public string UserName { get; set; }
  }

但是,這樣會(huì)提示我們一個(gè)錯(cuò)誤,就是類(lèi)中存在重復(fù)的屬性,是的,部分類(lèi)中,屬性是不可以重名的。那么,我們?cè)撛趺崔k呢,MVC框架已經(jīng)給了我們解決方案了。

我們可以這么寫(xiě):

?
1
2
3
4
5
6
7
8
9
[MetadataType(typeof(MeteUserInfo))]
  public partial class UserInfo
  {
    private class MeteUserInfo
    {
      [Required(ErrorMessage = "UserName不可為空1111")]
      public string UserName { get; set; }
    }
  }

這樣,我們上述的問(wèn)題就迎刃而解了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产精品无码中文字幕 | 国产一区二区三区水野朝阳 | 国产suv精品一区二区四区三区 | 东北美女野外bbwbbw免费 | 国产欧美日韩不卡 | 亚洲男人天堂 | 精品国产免费 | 亚洲邪恶天堂影院在线观看 | 蜜桃麻豆 | 日本丰满www色 | 男人天堂bt | 午夜AV亚洲一码二中文字幕青青 | 欧美理论片手机在线观看片免费 | 精品无码国产AV一区二区三区 | 丝瓜草莓香蕉绿巨人幸福宝 | 瘦老汉gay | 乌克兰13一14娇小 | 午夜伦理电影在线观免费 | 武侠古典久久亚洲精品 | yjsp妖精视频在线观看免费 | ai换脸杨颖被啪在线观看 | 国产精品国产高清国产专区 | 欧美日韩国产一区二区三区欧 | 亚洲一区二区三区深夜天堂 | 免费人成在线观看视频播放 | 色一情一乱一伦 | 91在线播| 国产99精品成人免费视频 | 色婷婷天天综合在线 | 日本人添下面的全过程 | 亚洲va国产日韩欧美精品色婷婷 | 91精品天美精东蜜桃传媒免费 | 成人影院免费在线观看 | 天美影视文化传媒mv免费 | 亚洲h片 | 91污污视频| 亚洲国产成人久久综合一区77 | 天天欲色成人综合网站 | 女人pp被扒开流水了 | 香蕉国产人午夜视频在线观看 | 高清国语自产拍免费视频国产 |