9

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!

ekad
  • 14,436
  • 26
  • 44
  • 46
Tomer Aro
  • 175
  • 1
  • 12
  • 1
    you are on the right path. Use the MotorcycleViewModel in the Controller. You can simply generate a CRUD Controller via the VS Templates with this type and see how it is being consumed. – kanchirk May 09 '15 at 21:06
  • Is there a reason you split your DbContext? If both `Posts` and `Comments` were a part of the same DbContext you wouldn't need to choose. If I had a large set of entities I might decide to split but then you'd need to be careful to group related entities together so you don't run into this problem. – Jasen May 09 '15 at 21:17
  • i must split it to 2 databases.. Kanchirk, i can't understood in which type will be the controller, cause whet i create it i need to select "date context class" and i had two - in both models.. – Tomer Aro May 09 '15 at 21:28
  • [ViewModels](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) are not Entities and are not a member of a `DbContext`. – Jasen May 09 '15 at 21:50
  • @TomerAro what do you want to display in your view, your viewmodel will depend upon your view requirement. – Dragon Sep 14 '15 at 08:38

4 Answers4

1

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.

dmyoko
  • 647
  • 4
  • 9
1

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

Rakesh Jena
  • 159
  • 1
  • 8
0

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

0

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

Anupam Shukla
  • 35
  • 2
  • 9