内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何對(duì)MVC進(jìn)行數(shù)據(jù)驗(yàn)證

這篇文章主要介紹如何對(duì)MVC進(jìn)行數(shù)據(jù)驗(yàn)證,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、天津網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為天津等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

一、一般情況

對(duì)于使用過MVC框架的人來說,對(duì)MVC的數(shù)據(jù)驗(yàn)證不會(huì)陌生,比如,我有一個(gè)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">
    <h5>UserInfo</h5>
    <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>
}

效果:

如何對(duì)MVC進(jìn)行數(shù)據(jù)驗(yàn)證

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

如果僅僅是這樣就完事了,那么也就沒事么意思了。

二、常用情況

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

假如:

public class UserInfo
  {  
    public string UserName { get; set; }
    public string Sex { get; set; }
    public string Mobile { get; set; }
    public string Address { get; set; }
  }

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

大家通常會(huì)想到部分類,是的,我們可以通過部分類來解決上述問題。

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

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

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

我們可以這么寫:

[MetadataType(typeof(MeteUserInfo))]
  public partial class UserInfo
  {
    private class MeteUserInfo
    {
      [Required(ErrorMessage = "UserName不可為空1111")]
      public string UserName { get; set; }
    }
  }

以上是“如何對(duì)MVC進(jìn)行數(shù)據(jù)驗(yàn)證”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前標(biāo)題:如何對(duì)MVC進(jìn)行數(shù)據(jù)驗(yàn)證
網(wǎng)頁網(wǎng)址:http://www.rwnh.cn/article36/ippepg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)公司、響應(yīng)式網(wǎng)站、營銷型網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司
民丰县| 恭城| 宁国市| 布拖县| 冀州市| 桃源县| 八宿县| 武宁县| 油尖旺区| 虞城县| 灵石县| 武强县| 陆川县| 南充市| 乐业县| 乌拉特中旗| 太谷县| 雅安市| 定兴县| 南康市| 四子王旗| 文昌市| 德保县| 博客| 慈溪市| 中牟县| 石林| 平遥县| 阳新县| 永修县| 密山市| 巴塘县| 新郑市| 开阳县| 彰武县| 桃江县| 奉节县| 文登市| 栖霞市| 滦南县| 会东县|