Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 851
Tomer Aro Avatar asked May 09 '15 21:05

Tomer Aro


People also ask

Can one controller have multiple models?

In MVC we cannot pass multiple models from a controller to the single view.

Can we use 2 models in a view?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.

Can a controller have multiple views?

Yes You can use multiple View in one Controller. so I have one controller and 2 views.

Why TempData is used in MVC?

What is TempData and How to Use in MVC? TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.


2 Answers

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

like image 165
Rakesh Jena Avatar answered Oct 05 '22 08:10

Rakesh Jena


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.

like image 22
dmyoko Avatar answered Oct 05 '22 08:10

dmyoko