如何显示其他班级的班级名称?

时间:2020-04-19 05:13:20

标签: c# asp.net database entity-framework asp.net-core-mvc

我有一个带有内部另一个类实例的类。

这是课程

 public class Contact
        {
            public int ContactId { get; set; }

            public string CategoryId { get; set; }

//here is instance of other class
            public Category Category { get; set; }


            [Required(ErrorMessage = "Please enter a name.")]
            public string FirstName { get; set; }

            [Required(ErrorMessage = "Please enter a name.")]
            public string LastName { get; set; }

            [Required(ErrorMessage = "Please enter a phone number")]
            [Phone(ErrorMessage = "Invalid phone number")]
            //[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid phone number")]
            public string Phone { get; set; }


            [Required(ErrorMessage = "The email address is required")]
            [EmailAddress(ErrorMessage = "Invalid email address")]
            public string Email { get; set; }
        }

这是另一个班级

public class Category
    {
        public string CategoryId { get; set; }
        public string Name { get; set; }
    }

在我的一种观点中,我有这样的模型

@model List<Contact>
@{
    ViewBag.Title = "Contacts";
}

并且我使用此代码在同一页面上显示类别名称,并且有效

<tbody>
        @foreach (var contact in Model)
        {
             <td>@contact.Category.Name </td>
        }
</tbody>

当我转到下一页时,我只使用一个联系人,而不是列表

@model Contact
@{
    ViewBag.Title = "Detail";
}

我使用此代码在同一页面上显示类别名称,但出现错误

<td>Category: </td>
<td>@Model.Category.Name</td>

这是我得到的错误。

NullReferenceException: Object reference not set to an instance of an object.

    AspNetCore.Views_Contact_Detail.ExecuteAsync() in Detail.cshtml

            <td>@Model.Category.Name</td>

这是我的ContactContext类

public class ContactContext : DbContext
    {
        public ContactContext(DbContextOptions<ContactContext> options)
            : base(options)
        { }
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<Category> Categorys { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Category>().HasData(
                new Category { CategoryId = "A", Name = "Friend" },
                new Category { CategoryId = "B", Name = "Family" },
                new Category { CategoryId = "C", Name = "Work" }
            );

            modelBuilder.Entity<Contact>().HasData(
                new Contact
                {
                    ContactId = 1,
                    FirstName = "Saul",
                    LastName = "Hudson",
                    Phone = "555-555-5555",
                    Email = "slash@gnr.com",
                    CategoryId = "A"
                },
                new Contact
                {
                    ContactId = 2,
                    FirstName = "Duff",
                    LastName = "Mckagan",
                    Phone = "555-555-1234",
                    Email = "duff@gnr.com",
                    CategoryId = "A"
                },
                new Contact
                {
                    ContactId = 3,
                    FirstName = "Axl",
                    LastName = "Rose",
                    Phone = "555-123-1234",
                    Email = "axl@gnr.com",
                    CategoryId = "A"
                },
                new Contact
                {
                    ContactId = 4,
                    FirstName = "Izzy",
                    LastName = "Stradlin",
                    Phone = "123-456-7891",
                    Email = "izzy@gnr.com",
                    CategoryId = "B"
                },
                new Contact
                {
                    ContactId = 5,
                    FirstName = "Steven",
                    LastName = "Adler",
                    Phone = "456-789-1234",
                    Email = "izzy@gnr.com",
                    CategoryId = "B"
                },
                new Contact
                {
                    ContactId = 6,
                    FirstName = "Matt",
                    LastName = "Sorum",
                    Phone = "555-555-5555",
                    Email = "matty@gnr.com",
                    CategoryId = "B"
                }, 
                new Contact
                {
                    ContactId = 7,
                    FirstName = "Gilby",
                    LastName = "Clarke",
                    Phone = "666-666-6666",
                    Email = "clarke@gnr.com",
                    CategoryId = "C"
                }, 
                new Contact
                {
                    ContactId = 8,
                    FirstName = "Dizzy",
                    LastName = "Reed",
                    Phone = "777-777-7777",
                    Email = "dizzy@gnr.com",
                    CategoryId = "C"
                }
            );
        }
    }

让我知道如何解决此问题,谢谢您的帮助

0 个答案:

没有答案