How Can I Use Two Models In Same Controller(Using Entityframwork) And Use Each Model In Different Action?

I have made one model so far and added controller using EntityFramework, and made some list of books:

public class Books
    {
        public string ImageUr { get; set; }
        public string BookTitle { get; set; }
        public string ShortDescription { get; set; }
        public decimal Price { get; set; }
        public string Author { get; set; }
        public int ID { get; set; }
    }
 
    public class BooksDBContext : DbContext
    {
        public DbSet<Books> Book { get; set; }
    }

and im displaying that list in this action:

public ActionResult Books()
       {
           return View(db.Book.ToList());
       }

and my question is how can i make another model or database that will be passed into this action (viewpage for news about new books for example):

public ActionResult News()
        {
             View(db.News.ToList());
        }

and the model for news to be something like this:

public class News
        {
            public string Title{ get; set; }
            public string Subtitle { get; set; }
            public string Content{ get; set; }
            public int ID { get; set; }
        }

so when i go to my News view i`ll be able to create News item(like in the img below) filling these fields and ill be able to show them in view:

If someone can give some pointers i would really appreciate it.

I found a solution for my problem.

What was it?

Im using entity frame work, and i created a new table in SQL database and updated the .edmx file in visual studio, this displays a dialog whereby your can select the table you want to include, in my case News, and in turn created a new model called News in my project which represents the new table, then i made few actions for creating and viewing News.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.