在之前的文章中,我和你們討論如何用SingalR和數據庫通知來完成一個消息監控應用。html
在上一篇文章中,我介紹瞭如何在MVC中對MongoDB進行CRUD操做。web
今天,我將繼續介紹一些在開發中很是有用的MVC特性,以下:數據庫
BindAttribute
Remote
HandleError
HiddenInput
BindAttribute
使用BindAttribute的目的是限制用戶在提交form表單時使用合適且正確的值。當咱們提交一個表單時,就會檢查每個實體上綁定的特性。瀏覽器
假設咱們已經有下面一個Employee實體類:ide
-
public class Employee { public string Name { get; set; } public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
創建一個EmployeeController,裏面添加兩個Action:工具
-
[HttpGet] public ActionResult EmployeeRegister() { return View(); } [HttpPost] public ActionResult EmployeeRegister(Employee emp) { return View(); }
給第一個Action創建視圖:spa
運行這個應用,填寫註冊表單:翻譯
若是咱們提交表單,在第二個Action中,咱們會獲得下面的值:3d
如今若是咱們只想提交Email,Name和PhoneNo,而咱們不想提交地址,這時咱們能夠在實體類上添加以下特性:code
[Bind(Exclude="Address")] public class Employee { public string Name { get; set; } public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
-
BindAttribute要在System.Web.Mvc命名空間下使用,使用BindAttribute,咱們能夠在提交表單時對字段進行一些控制。在下面的圖中,咱們已經在提交的form數據中得不到Address的值了。
咱們也能夠將BindAttribute直接用在Action的參數中,像下面這樣:
Remote Attribute
假設咱們有一個註冊表單,裏面有郵箱文本框,當輸入郵箱後,咱們想檢查輸入的郵箱是否在數據庫中已經存在,若是存在,則不提交表單,這時咱們可使用RemoteAttribute,經過RemoteAttribute,咱們能夠在不用提交表單就能夠先進行一些服務端驗證。
咱們能夠在下面的例子中使用RemoteAttribute:
-
public class Employee { public string Name { get; set; } [Remote("CheckEmail","Employee",ErrorMessage="Email is already exist")] public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
RemoteAttribute的第一個參數是一個Action名字,第二個是Controller名字,第三個是若是郵箱已存在後顯示給用戶看的提示信息。當咱們輸入完郵箱後,CheckEmail方法將被執行並檢查郵箱是否存在。
-
public JsonResult CheckEmail(string Email) { //Check here in database if it exist in database return true else false. return Json(false, JsonRequestBehavior.AllowGet); }
下面是執行效果:
HandleError Attribute
咱們已經有不少方法在MVC中處理異常,好比用try catch,或者使用Filter,或者經過第三方庫好比elmah。可是MVC也提供了一個HandleErrorAttribute去處理異常,以下:
-
[HandleError()] public ActionResult CheckError() { int a = 10; int b = 0; int k = a / b; return View(); }
在web.config文件中,咱們添加以下兩行:
-
<customErrors mode ="On" defaultRedirect ="Error.cshtml"> </customErrors>
在shared文件夾下建立一個視圖Error.cshtml,而後運行程序,若是運行上面的CheckError()方法,你剛建立的Error.cshtml將會顯示出來。
咱們也可使用HandleErrorAttribute給不一樣類型的異常顯示不一樣的視圖頁面。
-
[HandleError(ExceptionType=typeof(DivideByZeroException),View="DivideByZeroErrorView")] [HandleError(ExceptionType = typeof(NullReferenceException), View = "NullRefrenceErrorView")] public ActionResult CheckError() { int a = 10; int b = 0; int k = a / b; return View(); }
HiddenInput Attribute
若是咱們想對用戶隱藏一些實體字段,咱們可使用HiddenInput特性。
-
public class Employee { [HiddenInput(DisplayValue=false)] public string Name { get; set; } [Remote("CheckEmail","Employee",ErrorMessage="Email is already exist")] public string Email { get; set; } public string Address { get; set; } public string PhoneNo { get; set; } }
在以上的實體中,我用HiddenInput特性去描述Name字段。這樣程序運行後在瀏覽器中Name字段將不在顯示。所以HiddenInput給咱們d 在實體字段上多了一些額外的控制。
總結
本人英語四級考了五次都未經過(重在參與),目前仍是三級水準,這篇文章是我第一次翻譯,也沒有藉助翻譯工具,僅憑本身的理解,只想將更多國外的新鮮知識和你們分享。若是翻譯的很差還請各位看官多多包含,若是翻譯的還行,請給小弟弟我一個推薦吧,以給小弟弟我繼續翻譯的動力。