Begivenhed
19. nov., 23 - 21. nov., 23
Deltag i onlinesessioner hos Microsoft Ignite, der er oprettet for at udvide dine færdigheder og hjælpe dig med at håndtere nutidens komplekse problemer.
Tilmeld dig nuDenne browser understøttes ikke længere.
Opgrader til Microsoft Edge for at drage fordel af de nyeste funktioner, sikkerhedsopdateringer og teknisk support.
Value | |
---|---|
Rule ID | MVC1004 |
Fix is breaking or non-breaking | Breaking |
A model bound parameter has the same name as one of its properties.
Model binding a complex parameter with a property that has the same name may result in unexpected binding behavior. Consider renaming the parameter, or using a binding attribute to specify a different name.
Consider the following code:
public class HomeController : Controller
{
public IActionResult Get(SearchModel search)
{
...
}
}
public class SearcModel
{
public string Search { get; set; }
}
In this model, the parameter and its property are both named Search
, which results in model binding attempting to bind the property as search.Search
. Naming a parameter and its property the same prevents binding to a value without a prefix such as a query that looks like ?search=MySearchTerm
.
public IActionResult Get(SearchModel model)
{
...
}
Renaming a parameter on a public type could be considered a breaking change since it changes a library's public API surface.
Bind
to specify the model binding prefix:public IActionResult Get([Bind(Prefix = "")] SearchModel search)
{
...
}
Warnings can be suppressed if you intend to use the parameter name as a prefix during model binding.
ASP.NET Core feedback
ASP.NET Core er et åben kildekode projekt. Vælg et link for at give feedback:
Begivenhed
19. nov., 23 - 21. nov., 23
Deltag i onlinesessioner hos Microsoft Ignite, der er oprettet for at udvide dine færdigheder og hjælpe dig med at håndtere nutidens komplekse problemer.
Tilmeld dig nu