dotnet / dotnet/aspnetcore

Scaffolding uses Id for Foreign Key dropdown instead of Name.

Open
#46,664 5 comments 0 reactions 0 assignees View on GitHub
area-mvc feature-scaffolding investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Scaffolding Razor Pages using Entity Framework(CRUD) v1.0.0.0

I have tried hundreds of different configurations to get scaffolding to use Name instead of Id for a foreign key dropdown. The only thing that worked was giving the name property a [Required] attribute. Once It had a Required attribute it worked! So, I am thinking this might be an issue because this attribute should not affect scaffolding in this manner.

```c#
public class Product
{
public int Id { get; set; }

[Required(ErrorMessage = "Name is required.")]
[StringLength(100, ErrorMessage = "Name can not exceed 100 characters.")]
public string Name { get; set; }

[Display(Name = "Category")]
[Required(ErrorMessage = "Category is required.")]
public int CategoryId { get; set; }

[ForeignKey("CategoryId")]
[Display(Name = "Category")]
public virtual Category? Category { get; set; }
}

public class Category
{
public int Id { get; set; }

[Display(Name = "Category")]
//[Required] -- Uncommenting this fixes the issue
public string? Name { get; set; }

}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.