溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)MVC數(shù)據(jù)驗(yàn)證

發(fā)布時(shí)間:2021-08-07 10:40:53 來源:億速云 閱讀:139 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)MVC數(shù)據(jù)驗(yàn)證,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

一、一般情況

對(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()
  <div class="form-horizontal">
    <h5>UserInfo</h5>
    <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>
}

效果:

 如何實(shí)現(xiàn)MVC數(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; }
    }
  }

關(guān)于“如何實(shí)現(xiàn)MVC數(shù)據(jù)驗(yàn)證”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

mvc
AI