What does a block block actually do? How to use it effectively?

I studied something and came across this post on the buildsteded.com blog post about model bindings. It actually works well for my purposes, but I don’t know exactly what is going on behind the scenes. What I did was create a custom ModelBinder called USerModelBinder :

 public class UserModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult value = bindingContext.ValueProvider.GetValue("id"); MyEntities db = new MyEntities(); User user = db.Users.SingleOrDefault(u => u.UserName == value.AttemptedValue); return user; } } 

Then in my Global.asax.cs I have:

ModelBinders.Binders.Add(typeof(User), new UserModelBinder());

I understand that using model binding allows me NOT to use the following lines in every controller action that includes a “User”. Therefore, instead of passing the "id" to the action, the interceptor model intercepts the identifier, selects the correct "element" ("User in my case") and redirects it to the action for processing.

  MyEntities db = new MyEntities(); User user = db.Users.SingleOrDefault(u => u.UserName == value.AttemptedValue); 

I also tried using annotation in my User class instead of using a string in Global.asax.cs:

 [ModelBinder(typeof(UserModelBinder))] public partial class User { } 

I am not looking for a 30-page technical documentation, but I do not know how this works. I just want to understand what happens from the moment a request is submitted to the time it is submitted. All of this “just working” is unacceptable to me, lol. Also, is there a difference between using annotation and adding it to Global.asax.cs? They seem to work the same in my testing, but are there any bugs?

+4
source share
2 answers

Typically, the Binder Model (in MVC) looks at your action method and sees what it needs (such as object types). He then tries to find the values ​​from the HTTP request (values ​​in the HTTP form, QueryString, Json, and possibly in other places, such as cookies, etc., Using ValueProviders). Then it creates a new object with the parameters that it retrieves.

IMO What you did is not really a "model binding". In the sense that you just read the identifier and retrieved the object from the database.

An example of a normal model binding:

 // class public class SomeClass { public int PropA {get;set;} public string PropB {get;set;} } // action public ActionResult AddSomeClass(SomeClass classToBind) { // implementation } // pseudo html <form action=""> <input name="PropA" type="text" /> <input name="PropB" type="text" /> </form> 

if you publish a form containing the correct values ​​(say you submit the form with PropA and PropB), the model’s middleware can determine that you submitted these values ​​on the form and created the SomeClass object.

If you really want to create a real working example, you should use a strongly typed View and use the HtmlHelper EditorFor (or EditorForModel) to create all the correct names that MVC needs.

-

for the MVC link, the default is DefaultModelBinder , and some (moreover, you can look in System.Web. Mvc). The defaultProviders that it uses are FormValueProvider and QueryStringValueProvider

So, as I said, how it works in the main is that by default the connecting device reads the model that the action receives (for example, SomeClass in the example), reads what values ​​they can read (for example, PropA and PropB) and queries ValueProviders for the correct property values.

Also, if I remember correctly, you can also see the value providers at runtime using the static ValueProviderFactories class.

+6
source

A ModelBinder considers the arguments of the selected signature of the Controller action method, then converts the values ​​from the ValueProvider to these arguments.

This happens when ControllerActionInvoker invokes an action related to ControllerContext because the Controller Execute() method told it.

For more information on the ASP.NET MVC runtime, see Understanding the MVC Application Runtime

+2
source

Source: https://habr.com/ru/post/1345968/


All Articles