Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net 5: Bind attribute with Include parameter - include is not a valid named attribute argument

I write code from the article and and there is:

public IActionResult Create([Bind(Include="Imie,Nazwisko,Stanowisko,Wiek")] Pracownik pracownik)
{
    blablablab
}

I want to compile but it shows error.

 include is not a valid named attribute argument.

But in the internet I saw a code similar to my code. Someone explain to me what's going on? Of course, I am using asp.net 5.

like image 422
Kurator125 Avatar asked Mar 01 '15 14:03

Kurator125


People also ask

Which is not a valid attribute parameter type?

Decimals while a basic type are not a primitive type and hence cannot be represented in metadata which prevents it from being an attribute parameter.

What is BindProperty attribute?

The [BindProperty] is an instruction to the model binding framework to bind the underlying property with the matching request parameter.

What is FromBody in .NET core?

[FromBody] attributeThe ASP.NET Core runtime delegates the responsibility of reading the body to an input formatter. Input formatters are explained later in this article. When [FromBody] is applied to a complex type parameter, any binding source attributes applied to its properties are ignored.

How do I bind a model to view in MVC core?

How does model binding work in ASP.NET Core MVC. In an empty project, change Startup class to add services and middleware for MVC. Add the following code to HomeController, demonstrating binding of simple types. Add the following code to HomeController, demonstrating binding of complex types.


1 Answers

In Asp.Net Core, the Include property no longer has a setter. You need to pass the list of bound properties using the constructor:

public BindAttribute(params string[] include)
{
    Include = include;
}

And in your case:

public IActionResult Create([Bind("Imie","Nazwisko","Wiek")] Pracownik pracownik)
like image 85
haim770 Avatar answered Oct 19 '22 20:10

haim770