ASP.Net MVC 1到多个绑定不起作用

时间:2021-09-19 03:19:50

I am new to MVC and I've got this annoying problem.

我是MVC的新手,我遇到了这个恼人的问题。

I am doing a small project to understand MVC better.

我正在做一个小项目来更好地理解MVC。

I am working in a code-first way.

我正在以代码优先的方式工作。

I've created these two classes:

我创建了这两个类:

public class Post
    {
        [Required]
        public int ID { get; set; }

        [Required]
        public string UserName { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string Text { get; set; }

        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }

        [Required]
        public PostCategoryType Category { get; set; }

        [Required]
        public string PictureUrl { get; set; }

        [Required]
        public string VideoUrl { get; set; }

        public virtual List<UserComment> Comments { get; set; }
    }

public class UserComment
    {
        [Required]
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string WebSite { get; set; }
        [Required]
        public string Comment { get; set; }
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }
    }

public class MyStoreDbContext : DbContext
    {
        public DbSet<Post> Posts { get; set; }
        public DbSet<UserComment> UsersComments { get; set; }
    }

in order for the classes to be generated into tables at the entity framework I simply added controllers - one for each class and than ran the program and it generated the two tables.

为了在实体框架中将类生成到表中,我只需添加控制器 - 每个类一个,然后运行程序并生成两个表。

The generated tables at the entity framework looks like this:

实体框架中生成的表如下所示:

CREATE TABLE [dbo].[Posts] (
    [ID]         INT            IDENTITY (1, 1) NOT NULL,
    [UserName]   NVARCHAR (MAX) NOT NULL,
    [Title]      NVARCHAR (MAX) NOT NULL,
    [Text]       NVARCHAR (MAX) NOT NULL,
    [Date]       DATETIME       NOT NULL,
    [Category]   INT            NOT NULL,
    [PictureUrl] NVARCHAR (MAX) NOT NULL,
    [VideoUrl]   NVARCHAR (MAX) NOT NULL,
    CONSTRAINT [PK_dbo.Posts] PRIMARY KEY CLUSTERED ([ID] ASC)
);



CREATE TABLE [dbo].[UserComments] (
    [ID]      INT            IDENTITY (1, 1) NOT NULL,
    [Name]    NVARCHAR (MAX) NOT NULL,
    [Email]   NVARCHAR (MAX) NOT NULL,
    [WebSite] NVARCHAR (MAX) NOT NULL,
    [Comment] NVARCHAR (MAX) NOT NULL,
    [Date]    DATETIME       NOT NULL,
    [Post_ID] INT            NULL,
    CONSTRAINT [PK_dbo.UserComments] PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [FK_dbo.UserComments_dbo.Posts_Post_ID] FOREIGN KEY ([Post_ID]) REFERENCES [dbo].[Posts] ([ID])
);


GO
CREATE NONCLUSTERED INDEX [IX_Post_ID]
    ON [dbo].[UserComments]([Post_ID] ASC);

I have a controller which have a data member of :

我有一个控制器,其数据成员包括:

private MyStoreDbContext db = new MyStoreDbContext ();

private MyStoreDbContext db = new MyStoreDbContext();

in which I want my posts and user comments from.

我想要我的帖子和用户评论。

Now to the problem: When I access :

现在问题:我访问时:

foreach(Post post in db.Posts)
{
  post.Comments <------ This is always null
}

I've tried inserting a record of post with id 1 for example. Than I inserted to the UserComments in which the post_id is the 1. (Both manually with VS Server Explorer); when I try the code above the comments are always null.

我试过插入一个id为1的帖子记录。比我插入到post_id为1的UserComments中。(两者都是手动使用VS Server Explorer);当我尝试上面的代码时,注释始终为null。

I even tried different approach in which I manually inserted at debug time this:

我甚至尝试过不同的方法,我在调试时手动插入:

UserComment comment = new UserComment... // Initialization
Post post = new Post.. // Initializing all but Comments
post.Comments = new List<UserComment>();
post.Comments.Add(comment);
db.Comments.Add(comment);
db.Posts.Add(post);
db.SaveChanges();

After all that, each and every post's comments property is null.

毕竟,每个帖子的评论属性都是空的。

What am I missing? Please help me as it is driving me crazy and accroding to other people's solutions to this problem online is exactly like this I cannot point out the problem.

我错过了什么?请帮助我,因为它让我疯狂,并在其他人的解决方案上解决这个问题在线就是这样我无法指出问题。

Thanks in advance to all the helpers!

提前感谢所有帮助者!

1 个解决方案

#1


0  

You need to add ID of Post in the UserComment class, like below:

您需要在UserComment类中添加Post的ID,如下所示:

public class UserComment
    {
        [Required]
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string WebSite { get; set; }
        [Required]
        public string Comment { get; set; }
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }  

        public int PostID { get; set; }

       [ForeignKey("PostID")]
       public virtual Post Post { get; set; }       

    }

Then, Create a instance of Post first, like below:

然后,首先创建一个Post实例,如下所示:

var newPost = new Post{
   UserName = "John",
   //
}

db.Posts.Add(newPost );
db.SaveChanges();

After that, create a instance of UserComment, assign the ID of newPost to it, see below:

之后,创建UserComment的实例,为其分配newPost的ID,如下所示:

var comment  = new UserComment
{
   Name  = "Some one",
   //
   PostID = newPost.ID
}
newPost.Comments.Add(comment);
db.SaveChanges();

Note: You can learn more about this at below link:

注意:您可以在以下链接中了解更多相关信息:

http://codeblog.shawson.co.uk/entity-framework-4-1-code-first-with-one-to-many-relationship-code-example/

#1


0  

You need to add ID of Post in the UserComment class, like below:

您需要在UserComment类中添加Post的ID,如下所示:

public class UserComment
    {
        [Required]
        public int ID { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string WebSite { get; set; }
        [Required]
        public string Comment { get; set; }
        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        public DateTime Date { get; set; }  

        public int PostID { get; set; }

       [ForeignKey("PostID")]
       public virtual Post Post { get; set; }       

    }

Then, Create a instance of Post first, like below:

然后,首先创建一个Post实例,如下所示:

var newPost = new Post{
   UserName = "John",
   //
}

db.Posts.Add(newPost );
db.SaveChanges();

After that, create a instance of UserComment, assign the ID of newPost to it, see below:

之后,创建UserComment的实例,为其分配newPost的ID,如下所示:

var comment  = new UserComment
{
   Name  = "Some one",
   //
   PostID = newPost.ID
}
newPost.Comments.Add(comment);
db.SaveChanges();

Note: You can learn more about this at below link:

注意:您可以在以下链接中了解更多相关信息:

http://codeblog.shawson.co.uk/entity-framework-4-1-code-first-with-one-to-many-relationship-code-example/