What is the purpose of the "[Bind (" ID, Title, ReleaseDate, Genre, Price ")]" in a method?

2

I'm creating a project in ASP.NET Core MVC for learning purposes. In a given part of the Microsoft guide when the scaffold technique is approached to generate the controller and views controller , I came across an instruction in some methods that left me with doubt in very confusing.

See below the Create method as an example:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
    if (ModelState.IsValid)
    {
        _context.Add(movie);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(movie);
}

View the statement before the declaration of the movie parameter. It is at this point that my doubt arose and it confused me.

Questions

  • What kind of feature is the statement [Bind("ID,Title,ReleaseDate,Genre,Price")] passed in signature of the Create method?
  • What would be the purpose of this feature when used in a method?
  • asked by anonymous 13.11.2017 / 01:44

    1 answer

    2
      

    What kind of characteristic is the [Bind ("ID, Title, ReleaseDate, Genre, Price")] statement passed in the Create method signature?

    These are attributes. Can be used in other parts of the code .

      

    What would be the purpose of this feature when used in a method?

    It is a way to define metadata for plication. This can be used by the compiler, the general framework (.NET), the specific framework (ASP.NET for example) or even your own application.

    This information can be obtained with reflection and used in the most convenient way.

    In this case ASP.NET Core takes the information there and maps to the object, then the strings received by HTTP with the names quoted there will be used to compose the object movie . The framework knows how to do this, you just need to know how to use the attribute to indicate what it should do.

    It's up to a security measure against tampering .

    This is a more declarative form of programming which is useful in some cases to reduce

    13.11.2017 / 13:05