简体   繁体   中英

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:

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" . 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. 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.

Unless I'm missing something here, you don't need a ViewModel... at least not to solve this problem. ViewModel are useful when concerning with organization.

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.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

In this scenario you don't need to pass two models in view. You can simply pass the Comment model. However you can use Tuple for passing multiple models in view . Here is a great example of CRUD operation using Tuple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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