Model Binder stay Asp.net
MVC It's very simple . In short, it's in your controller Action Method requires parameter data ; These parameters are included in the HTTP On request , Include the Value and URL Parameters in, etc . and ModelBinder The function of Value and URL Change the parameters in to objects , These objects are then bound to the Action Parameters above . I drew a simple picture , It will look more intuitive .
stay asp.net mvc You can write code like this :
[HttpPost]
public ActionResult Create()
{
Book book = new Book();
book.Title = Request.Form["Title"];
// ...
return View();
}
But this kind of writing is very undesirable , Because the code is not easy to read , It's not easy to test . Look at the writing below :
[HttpPost]
public ActionResult Create(FormCollection values)
{
Book book = new Book();
book.Title = values["Sex"];
// ...
return View();
}
In this way, it is not necessary to Request We got the data in , This can satisfy some situations , Than directly from Request Data acquisition should be intuitive . But if Action The data you need comes from both the values on the form , Come from again URL Of query
string. In this case, only FormCollection No way . Look at the code below :
[HttpPost]
public ActionResult Create(Book book)
{
// ...
return View();
}
The above code is very intuitive , It needs us model
binder Create a book object , It then takes values directly from the properties of the object . this book The object's data naturally comes from Form and URL. occasionally , ours DefaultModelBinder The ability to transform must be limited , It's not transparent enough , Some special and complex situations need to be customized Model
Binder. Let me talk about how to customize Model Binder.
1, First, we define a Book Entity class of :
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public DateTime DatePublished { get; set; }
}
2, Custom model binder Need to inherit IModelBinder Or a subclass of it . Data can be downloaded from bindingContext obtain .
public class BookModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext,
ModelBindingContextbindingContext)
{
var book = (Book)(bindingContext.Model ?? new Book());
book.Title = GetValue<string>(bindingContext, "Title");
book.Author = GetValue<string>(bindingContext, "Author");
book.DatePublished = GetValue<DateTime>(bindingContext, "DatePublished");
if (String.IsNullOrEmpty(book.Title))
{
bindingContext.ModelState.AddModelError("Title", " Book title cannot be empty ?");
}
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));
}
}
As you can see from the code above , Custom ModelBinde Very free , Free to Form The one on the key An attribute corresponding to the entity , You can also add some verification logic . Of course, you can also add some other custom logic .
3, Write well BookModelBinder after , We just need a simple registration , stay Global.asax Add the following code :
ModelBinders.Binders.Add(typeof(Book), new BookModelBinder());
summary : This paper gives a brief introduction Asp.net MVC Of Model Binder mechanism . If there is a problem with the narrative , Welcome to correct .
Technology