2

I was learning .NET MVC 5 and had a question about the square brackets when using ModelBinder.

[HttpPost]
        public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer cust)
        {
            return View("Customer", cust);
        }

I don't understand how the square brackets work on objects. how the data from the form is transferred to the CustomerBinder? and in general what is the flow of the code of the Submit action parameter.

thanks in advance :)

tereško
  • 58,060
  • 25
  • 98
  • 150
ReDragon
  • 43
  • 1
  • 5
  • 2
    Those are called attributes, and what they do is explained in the tutorial you follow. If not, you need to find another tutorial. :) Where did you obtain this code? Explaining the entire flow of MVC is a bit too broad, and that too is documented: [MSDN: Understanding the MVC Application Execution Process](https://msdn.microsoft.com/en-us/library/dd381612(v=vs.100).aspx), [SO: Execution flow in MVC](http://stackoverflow.com/questions/1982517/), [ASP.NET: Lifecycle of an ASP.NET MVC 5 Application](http://www.asp.net/mvc/overview/getting-started/lifecycle-of-an-aspnet-mvc-5-application). – CodeCaster Dec 04 '15 at 18:30
  • 1
    Also, if you're just starting out with MVC, you want to stay away from custom model binders. Just use the default model binder. If you want to learn more about model binders, see [MSDN: Models and Validation in ASP.NET MVC](https://msdn.microsoft.com/en-us/library/dd410405(v=vs.100).aspx). – CodeCaster Dec 04 '15 at 18:34
  • Some body say the square brackets, [] , in this context means C# attribute, data annotation other wise referred to as declarative programming. You declare what you want as an attribute and the compiler takes care of it – Julius Depulla Dec 04 '15 at 18:44

3 Answers3

4

These are called attributes, and there is an msdn turotial explaining them here.

Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection

EDIT:

To specifically address how MVC uses attributes, I would recommend continuing your MVC tutorial as this is a longer answer. If you would like one that I've found, here's a good one.

Briefly,

  • The [HttpPost] attribute let's MVC know only to allow POST requests to come through to this method, and not other Http Verbs like PUT or GET.
  • The [ModelBinder(typeof(CustomerBinder))] attribute informs the controller as to the binder that you want it to use for the object that gets passed in.
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
1

In this particular code:

[HttpPost]
public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer cust)
{
    return View("Customer", cust);
}

These are the parts:

  • [HttpPost] is a method attribute. It modifies the method with additional meta-data or meta-functionality. In this case it restricts HTTP access to that action to only POST verbs.
  • [ModelBinder(typeof(CustomerBinder))] is another attribute, but this modifies the particular argument to the method instead of the method itself. In this case the Customer cust argument. The ModelBinder attribute allows you to explicitly specify a model binder to use, so you can provide custom model binders for particular actions.

Essentially, MVC (and WebAPI) examines the incoming form values in the HTTP request and makes its best effort (using default model binders) to apply those values to the method arguments. In the vast majority of cases, this works just fine. Sometimes, however, you may want to implement custom functionality for this. So you can write your own model binders, and there are varying ways to tell the framework to use them. This is one such way, which applies a specific model binder (CustomerBinder) to a specific argument of only this specific method.

David
  • 208,112
  • 36
  • 198
  • 279
0

square brackets just are attributes. httppost and httpe get just limit the action to request type.

you can se link below for mor info: https://msdn.microsoft.com/en-us/library/z0w1kczw%28v=vs.80%29.aspx

virtouso
  • 549
  • 11
  • 30