Scaffolding uses Id for Foreign Key dropdown instead of Name.
- 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
Assessment
This issue has not been assessed yet.