MVC 模型繫結

來源:互聯網
上載者:User

標籤:

  在WebForm,擷取提交表單的值一般都是Request.Form["Title"]這樣的方式。在MVC中,提供了模型繫結機制。讓後台擷取表單或Url中的參數變得更加簡單。

一、基本模型繫結

  你可以直接在參數中用字串,整型變數,實體或者是List<實體>的方式擷取表單提交的參數。

  參數中的這些東西都是與表單中的Html控制項的name屬性一一對應的。

        public ActionResult PersonAdd(int Id)        {            return View();        }

  例如以上代碼,它能夠匹配Url中的Id參數。如以下兩種方法Id都能夠匹配到1

  http://localhost/Home/PersonAdd/1  http://localhost/Home/PersonAdd?Id=1

  在例如如下代碼:

        public ActionResult PersonAdd(string Name)        {            return View();        }

  它能夠匹配到表單中提交的張三:

<input type="text" name="Name" value="張三" />

  也能夠匹配到Get請求的路徑參數:

http://localhost/Home/PersonAdd?Name=張三

  如果是用實體,則會檢查該實體的屬性名稱與表單中name屬性中對應的標籤的值。

  例如有如下實體:

    public class Person_Model    {        public int Id { get; set; }        public string Name { get; set; }    }

  在Controller中的參數填寫如下:

        [HttpPost]        public ActionResult PersonAdd(Person_Model model)        {            if (ModelState.IsValid)  //此處僅作示範,不考慮安全性            {                //插入資料庫省略                return Redirect("/Home/PersonManager");            }            return View();        }

  這樣的話,模型繫結器會自動檢查該實體的屬性與Name一一對應的標籤並綁定。如下表單的值將被綁定到model實體的屬性中。

  <input type="hidden" name="Id" value="1" />  <input type="text" name="Name" value="張三" />
二、顯式模型繫結

  UpdateModel與TryUpdateModel都用於顯示模型繫結。如果綁定期間出現錯誤或者模型是無效的。

  UpdateModel將拋出一個異常。因此UpdateModel要用try catch語句塊包起來,而TryUpdateModel不會拋出異常,而是返回一個布爾類型的值,true表示綁定成功,false表示綁定失敗。如:

        [HttpPost]        public ActionResult PersonAdd()        {            Person_Model model = new Person_Model();            try            {                UpdateModel(model);                //插入資料庫                return Redirect("/Home/PersonManager");            }            catch            {                return View(model);            }        }

  TruUpdateModel:

        [HttpPost]        public ActionResult PersonAdd()        {            Person_Model model = new Person_Model();            if (TryUpdateModel(model))            {                //插入資料庫                return Redirect("/Home/PersonManager");            }            else            {                return View(model);            }        }

  另外,模型繫結還有一個模型狀態,模型繫結器一斤模型中的每一個值在模型狀態中都有相應的一條記錄。可以隨時查看綁定狀態。如:

        [HttpPost]        public ActionResult PersonAdd()        {            Person_Model model = new Person_Model();            TryUpdateModel(model);            if (ModelState.IsValid)            {                //if(ModelState.IsValidField("Name"))                //插入資料庫                return Redirect("/Home/PersonManager");            }            else            {                return View(model);            }        }
三、安全問題:重複提交

   假設有如下實體:

    public class Comment    {        public int Id { get; set; }        //評論者姓名        public string Name { get; set; }        //評論內容        public string Content { get; set; }        //是否已審核        public bool Approved { get; set; }    }

  在Controller中:

        public ActionResult CommentAdd(Comment com)        {            if (ModelState.IsValid)            {                //添加資料庫                return Redirect("/Home/CommentManager");            }            else            {                 return View(com);            }        }

  在以上代碼中,如果有惡意使用者在表單資料中添加"Approved=true"來幹預表單的提交,那麼該評論將是預設就通過審核的。這時候我們可以使用Bind特性來防禦重複提交攻擊。

  白名單:

[Bind(Include="Name,Content")]      //白名單,只綁定這兩個屬性[Bind(Exclude="Id,Approved")]        //黑名單,不綁定這兩個屬性

  Bind特性可以應用於參數左側也可以應用於實體Model類的頂部,應用於實體Modle的頂部則是對所有該實體綁定有效,而應用於參數左側則只是對該action中的請求有效。

  如:

public ActionResult CommentAdd([Bind(Exclude="Approved")]Comment com) {   if (ModelState.IsValid)   {     //添加資料庫     return Redirect("/Home/CommentManager");   }   else   {     return View(com);   } }

  另外,UpdateModel與TryUpdateModel也有一個重載版本來接收一個繫結資料行表:

UpdateModel(com, "", new string[] { "Id", "Name", "Content" });

  最後,還有一種就是視圖模型,即另外在定義一個模型來專供視圖使用,僅僅包括需要綁定的屬性。

  另外,如果兩個類有相同的Name屬性,要同時綁定,區分HTML可以這樣寫:

<p>客戶名稱: <input type="text" name="customer.Name" style="width: 300px" /></p><p>銷售人員名稱: <input type="text" name="salesman.Name" style="width: 300px" /></p>

 

三、模型繫結原理

  在ASP.NET MVC中,使用者請求道伺服器的資料將被封裝為Model資料對象,這個資料對象通常也被View用來提供顯示的資料。在ASP.NET MVC中,提供了非常靈活的Model綁定機制,通過IModelBinder借口,定義了綁定Model資料的約定,並提供了一個介面的預設實現DefaultModelBinder。在大多數情況下,僅僅通過DefaultModelBinder就可以完成Model的綁定。

  如果需要的話,也可以自訂一個IModelBinder的實現,完成特定類型的Model綁定。

public interface IModelBinder{    object BindModel(ControllerContext controllerContext,ModelBindContext bindingContext);}

  1、綁定Model

  預設情況下,ASP.NET MVC使用DefaultModelBinder來綁定Model的資料。在傳遞Action參數的時候,ASP.NET MVC按照如下順序尋找匹配的資料:

  1. form表單中的資料;
  2. RouteData中的資料;
  3. QueryString中的資料;

  2、簡單參數和複雜參數

  如果Action方法的參數類型是實值型別和字串類型,那麼DefaultModelBinder將尋找與Action參數名稱匹配的參數,如果沒有對應的參數,那麼Action的參數將試圖賦予Null 參考。因此,對於簡單類型的參數來說,參數的類型應該是可空的。

  多數情況下,我們會通過一個Model對象來處理複雜的參數,DefaultModelBinder會遍曆Model對象的屬性來綁定參數。
  如果不希望DefaultModelBinder對某個參數進行綁定,可以通過BindAttribute進行說明,其中定義了三個屬性:

  • Include表示需要綁定的屬性,各個屬性之間以逗號進行分隔。
  • Exclude表示不需要綁定的屬性,各個屬性之前以逗號分隔。
  • Prefix表示請求參數的首碼。

  這些標籤可以定義在Model上,說明在參數綁定過程中需要綁定的屬性或者不需要綁定的屬性,如:

[Bind(Include = "Name,Birthday")]public class Person{  public int Id { get; set; }  public string Name { get; set; }  public DateTime Birthday{ get; set; }}

   在UpdateModel方法中,指定包含的屬性和不包含的屬性。

UpdateModel(  person,     //Model  "person",    //Prefix  new[] { "Id","Name" },  //Include  new [] { "Birthday" }   //Exclude);

MVC 模型繫結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.