asp.net web開發框架,ASP.NET MVC2之Model Binder

 2023-12-06 阅读 26 评论 0

摘要:Model Binder在Asp.net MVC中非常簡單。簡單的說就是你控制器中的Action方法需要參數數據;而這些參數數據包含在HTTP請求中,包括表單上的Value和URL中的參 數等。而ModelBinder的功能就是將這些個表單上的Value和URL中的參數換成對象,然后將這些對象綁定

Model Binder在Asp.net MVC中非常簡單。簡單的說就是你控制器中的Action方法需要參數數據;而這些參數數據包含在HTTP請求中,包括表單上的Value和URL中的參 數等。而ModelBinder的功能就是將這些個表單上的Value和URL中的參數換成對象,然后將這些對象綁定到Action的參數上面。我簡單的 畫了一個圖,看起來會更加直觀。

  在asp.net mvc中你可以寫類似下面這樣的代碼:

[HttpPost]
public ActionResult Create()
{
Book book = new Book();
book.Title = Request.Form["Title"];
// ...
return View();
}

asp.net web開發框架,  但是這樣的寫法是非常不可取的,因為代碼不容易閱讀,也不易測試。再看下面的寫法:

[HttpPost]
public ActionResult Create(FormCollection values)
{
Book book = new Book();
book.Title = values["Title"];
// ...
return View();
}

  這樣的寫法就可以不用從Request中獲取數據了,這樣能滿足一些情況,比直接從Request中獲取數據要直觀。但是如果在Action需要的數據既要來自表單上的值,又要來自URL的query string。這種情況單單FormCollection是不行的。看下面代碼:

[HttpPost]
public ActionResult Create(Book book)
{
// ...
return View();
}

  上面的代碼就非常的直觀了,這需要我們的model binder創建一個book對象,然后直接從這個對象的屬性中取值。這個book對象的數據自然也是來自Form和URL。有時候,我們的 DefaultModelBinder轉換的能力必經有限,也不夠透明化,一些特殊和復雜的情況就需要我們自定義Model Binder。下面我講講如何去自定義Model Binder。

  1、首先我們定義一個Book的實體類:

public class Book
{
public string Title { get; set; }
public string Author { get; set; }
}

ASPNET程序開發招聘、  2、自定義的model binder需要繼承IModelBinder或者它的子類。數據可以從bindingContext獲取。

public class BookModelBinder : IModelBinder
{

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var book = (Book)(bindingContext.Model ?? new Book());
book.Title = GetValue<string>(bindingContext, "Title")+"www";//這里在獲取到表單的Title信息后隨便追加個"www",用來測試的
book.Author = GetValue<string>(bindingContext, "Author");
if (String.IsNullOrEmpty(book.Title))
{
bindingContext.ModelState.AddModelError("Title", "書名不能為空?");//添加錯誤信息
}
return book;
}
private T GetValue<T>(ModelBindingContext bindingContext, string key)
{
ValueProviderResult valueResult= bindingContext.ValueProvider.GetValue(key);
bindingContext.ModelState.SetModelValue(key, valueResult);
return (T)valueResult.ConvertTo(typeof(T));
}
}

  從上面代碼可以看出,自定義的ModelBinde非常的自由,可以自由的將Form上的一個key對應實體的一個屬性,也可以加入一些驗證的邏輯。當然還可以加入一些其他的自定義邏輯。

  3、寫好BookModelBinder之后,我們只需要簡單的注冊一下就行了,在Global.asax添加下面代碼:

    protected void Application_Start()
??????? {
??????????? ModelBinders.Binders.Add(typeof(Book), new BookModelBinder());
??????????? AreaRegistration.RegisterAllAreas();

??????????? RegisterRoutes(RouteTable.Routes);
??????? }

  4、View中的表單代碼:

ASP.NET MVC4開發指南?    <form action="Show" method="post">
??????????? 書名:<input type="text" name="Title"/><br />
??????????? 作者:<input type="text" name="Author" /><br />
??????????? <input type="submit" value="提交" />
??????? </form>

  5、Controller中對應的Action的代碼:

   public ActionResult Show(Book book)
??????? {
??????????? return View();
??????? }

  在Action中可以通過添加斷點來查看book對象中的信息,是通過ModelBinder來進行綁定信息的。

ASP.NET。  總結:本文簡單介紹了一下Asp.net MVC的Model Binder機制。如果敘述有問題,歡迎指正

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/189953.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息