I have found that if scaffolding a class that contains a Bool value then incorrect syntax is generated on the Create and Edit models (this issue also occurred in .Net Core 2.0):
Steps to reproduce:
public class Customer
{
public int CustomerID { get; set; }
...
[Display(Name = "Can Contact?")]
public bool CanBeContacted { get; set; }
...
}
Then scaffold the class and if you try to build the project it will fail with the following error:
'EditModel' does not contain a definition for 'CanBeContacted' and no accessible extension method 'CanBeContacted' accepting a first argument of type 'EditModel' could be found (are you missing a using directive or an assembly reference?)
The workaround is to change the line in the Create/Edit model from:
<input asp-for="Customer.CanBeContacted" /> @Html.DisplayNameFor(model => model.CanBeContacted)
To:
<input asp-for="Customer.CanBeContacted" /> @Html.DisplayNameFor(model => model.Customer.CanBeContacted)
After this the project builds successfully.
@robinwilson16 - This has been fixed by #709.
It's not fixed though in the latest version from what I can tell.
Looking at #709 it looks like it may have only been fixed in 2.0 with the fix not ported forward to 2.1?
Are there plans to fix in 2.1 as well?
It also adds an extra closing bracket which just ends up appearing as text on the page.
I do hit it every time I add a bool value and can replicate every time. Visual Studio is fully up to date.
I installed .Net Core 2.2 last night and see the same issue with bool properties in the model.
I know, not sure why this one is so hard to fix. Trying to scaffold a model will always mess up on bool values generating an incorrectly scaffolded model which you must correct by hand otherwise your project doesn't even compile after scaffoling. It looks like they only fixed the issue in 2.0 but not in 2.1 and the bug has carried over into 2.2. I think the template file probably just needs correcting. Maybe I should have been more specific it was 2.1 in my initial post.
This is fixed in the latest version. Thanks!
Have just logged a new issue as this is still not fully fixed as still generates the additional closing bracket in the create model that shouldn't be there - aspnet/Scaffolding#1212
It doesn't cause syntax errors though like it used to so things have improved.
Most helpful comment
It's not fixed though in the latest version from what I can tell.
Looking at #709 it looks like it may have only been fixed in 2.0 with the fix not ported forward to 2.1?
Are there plans to fix in 2.1 as well?
It also adds an extra closing bracket which just ends up appearing as text on the page.
I do hit it every time I add a bool value and can replicate every time. Visual Studio is fully up to date.