MVC數(shù)據(jù)驗證詳解
一、一般情況
對于使用過MVC框架的人來說,對MVC的數(shù)據(jù)驗證不會陌生,比如,我有一個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可以通過對一些屬性添加一定的特性來對數(shù)據(jù)進(jìn)行驗證。這對大家來說可能并不陌生。
如果僅僅是這樣就完事了,那么也就沒事么意思了。
二、常用情況
在實際的開發(fā)中,我們大都是通過EF,或者其他方式,使得數(shù)據(jù)庫中的每一個表或視圖,都在代碼中對應(yīng)的一個類模型,對于通過數(shù)據(jù)庫生成的模型,我們不宜修改,退一步講,即使我們在這個類中對一些屬性增加一些數(shù)據(jù)驗證的特性,那么,數(shù)據(jù)庫發(fā)生變化后,如果我再重新生成這些Model,我們之前添加好的驗證特性將沒有了,那么,我們?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ù)庫生成的一個模型,對于數(shù)據(jù)庫生成的模型,我們不宜修改。但那是,我們又需要對這個模型中的某些屬性進(jìn)行數(shù)據(jù)驗證,比如需要對UserName屬性進(jìn)行非空驗證,那么我們?nèi)绾巫瞿兀?/p>
大家通常會想到部分類,是的,我們可以通過部分類來解決上述問題。
首先,我們將模型中的類加上關(guān)鍵字 partial ,然后我們再寫一個這個模型的部分類。
public partial class UserInfo
{
[Required(ErrorMessage = "UserName不可為空1111")]
public string UserName { get; set; }
}
但是,這樣會提示我們一個錯誤,就是類中存在重復(fù)的屬性,是的,部分類中,屬性是不可以重名的。那么,我們該怎么辦呢,MVC框架已經(jīng)給了我們解決方案了。
我們可以這么寫:
[MetadataType(typeof(MeteUserInfo))]
public partial class UserInfo
{
private class MeteUserInfo
{
[Required(ErrorMessage = "UserName不可為空1111")]
public string UserName { get; set; }
}
}
這樣,我們上述的問題就迎刃而解了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
將Excel中數(shù)據(jù)導(dǎo)入到Access數(shù)據(jù)庫中的方法
將Excel中數(shù)據(jù)導(dǎo)入到Access數(shù)據(jù)庫中的方法,需要的朋友可以參考一下2013-03-03
ASP.NET MVC中jQuery與angularjs混合應(yīng)用傳參并綁定數(shù)據(jù)
這篇文章主要介紹了ASP.NET MVC中jQuery與angularjs混合應(yīng)用傳參并綁定數(shù)據(jù),需要的朋友可以參考下2017-06-06
.Net微信開發(fā)之如何解決access_token過期問題
這篇文章主要為大家詳細(xì)介紹了.Net微信開發(fā)之如何解決access_token過期問題的方法,感興趣的小伙伴們可以參考一下2016-06-06
詳解如何在ASP.Net Core中實現(xiàn)健康檢查
這篇文章主要介紹了詳解如何在ASP.Net Core中實現(xiàn)健康檢查,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
ASP.NET實現(xiàn)可以縮放和旋轉(zhuǎn)的圖片預(yù)覽頁效果
本文詳細(xì)介紹了如何在ASP.NET?WebForms中實現(xiàn)一個功能豐富的圖片預(yù)覽頁面,通過結(jié)合HTML、CSS和JavaScript,用戶可以方便地對圖片進(jìn)行放大、縮小以及旋轉(zhuǎn)操作,感興趣的朋友跟隨小編一起看看吧2024-08-08
Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
Asp.net 取得文件后綴名,保存文件,加入文字水印的代碼類2008-11-11

