简体   繁体   English

ASP.Net使用一个控制器创建两个模型

[英]ASP.Net Create Two Models With One Controller

I'm just trying To create one controller that will work with two models. 我只是想创建一个可以使用两个模型的控制器。 Comment Model: 评论模型:

 public class Comment
 {
    public int ID { get; set; } // property
    public int PostID { get; set; }
    public String Title { get; set; }
    public String Name { get; set; }
    public Uri Url { get; set; }
    public String Text { get; set; }
    public Post Post { get; set; }
}

public class CommentDBContext : DbContext 
{
    public DbSet<Comment> Comments { get; set; }

    public System.Data.Entity.DbSet<BlogShauli.Models.Post> Posts { get; set; }

}

Post Model: 邮政模式:

  public class Post
  {
    public int ID { get; set; } // property
    public String Title { get; set; }
    public String Author { get; set; }
    public String AuthorSite { get; set; }
    public DateTime ReleaseDate { get; set; }
    public String Text { get; set; }
 }

public class PostDBContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
}

And now I want to create a Single Controller that will work with both models. 现在我想创建一个可以同时使用这两种模型的单一控制器。 I read that the way to do it is to use ViewModel Pattern so i created one more model class named " BlogViewModel.cs ", with the following code: 我读到这样做的方法是使用ViewModel Pattern,所以我创建了另一个名为“ BlogViewModel.cs ”的模型类,其代码如下:

public class MotorcycleViewModel
 {
     public Comment CommentPointer { get; set; }
     public Post PostPointer { get; set; }
 }

But from here i didn't understand what do. 但从这里我不明白做什么。 i'm trying to create a new Controller using Entity framework but i don't know what to select in the "Data context class" . 我正在尝试使用Entity框架创建一个新的Controller,但我不知道在“数据上下文类”中要选择什么。 can someone would explain me how to make the connection between Both models and the Controller? 谁能解释我如何在两个模型和控制器之间建立连接? Thanks! 谢谢!

You only need one controller: Post. 你只需要一个控制器:发布。

Since Comments are related to Post, you can create a relationship and map it using EF. 由于评论与Post相关,您可以创建关系并使用EF对其进行映射。 So your Post will have list of comments, that can be retrieved eagerly or lazyly, accordingly to your choice. 因此,您的帖子将包含评论列表,可以根据您的选择热切或懒惰地检索。 So google for EF One to Many Relationships, create a virtual property in your Post that is an IEnumerable and return it from any model. 所以谷歌的EF一对多关系,在你的帖子中创建一个IEnumerable的虚拟属性,并从任何模型返回它。

Unless I'm missing something here, you don't need a ViewModel... at least not to solve this problem. 除非我在这里遗漏了一些东西,否则你不需要ViewModel ......至少不能解决这个问题。 ViewModel are useful when concerning with organization. 在涉及组织时,ViewModel非常有用。

Please try the following in your repository class - 请在您的存储库类中尝试以下操作 -

public MotorcycleViewModel GetModelData(int commentId, int postId)
{
    MotorcycleViewModel result =new MotorcycleViewModel();
    using (var context = new CommentDBContext())
    {
       var post = (from pst in context.Post where pst.ID == postId  select pst).FirstOrDefault();
       var comment = (from cmt in context.Comment where cmt.ID == commentId select cmt).FirstOrDefault();
       result.CommentPointer = comment;
       result.PostPointer = post;
       return result;
    }
}

please follow the link to see how conversion happens from model to viewmodel and vice versa 请点击链接查看从模型到视图模型的转换方式,反之亦然

You can equally do it this way 你也可以这样做

@model MotorcycleViewModel

@Html.DisplayNameFor(x=>x.CommentPointer.Title) @Html.DisplayFor(x=>x.CommentPointer.Title) @Html.DisplayNameFor(x=>x.CommentPointer.Title) @Html.DisplayFor(x=>x.CommentPointer.Title)

@Html.DisplayNameFor(x=>x.PostPointer.Title) @Html.DisplayFor(x=>x.PostPointer.Title) @Html.DisplayNameFor(x=>x.PostPointer.Title) @Html.DisplayFor(x=>x.PostPointer.Title)

However this code helps you display data from both tables.. Moreover @TomerAro I'd advice you to use one context as multiple context could cause confusion 但是这段代码可以帮助你显示来自两个表的数据..而且@TomerAro我建议你使用一个上下文,因为多个上下文可能会引起混淆

In this scenario you don't need to pass two models in view. 在这种情况下,您不需要在视图中传递两个模型。 You can simply pass the Comment model. 您只需传递Comment模型即可。 However you can use Tuple for passing multiple models in view . 但是,您可以使用Tupleview传递多个模型。 Here is a great example of CRUD operation using Tuple 这是使用Tuple进行CRUD操作的一个很好的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM