0

I have Konu and Etiketler tables in database.

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        SonerSevincComEntities context = new SonerSevincComEntities();

        var Konular = context.Konu.ToList();
        return View(Konular);
    }

}

In Konu Controller's View ,

I want to call Etiketler table and its Id.

I already use Konu Model But i want to use Etiketler Model in 1 View.

How can i call 2 tables(models)in 1 view page ?

Soner
  • 1,280
  • 3
  • 16
  • 40

1 Answers1

0

You will need to create a ViewModel:

public class KonuEtiketlerViewModel
{
   public virtual List<Konu> Konus {get; set;}
   public virtual List<Etiketler> Etiketlers {get; set;}
}

and in your controller:

public ActionResult Index()
{
   SonerSevincComEntities context = new SonerSevincComEntities();
   var viewModel =  new KonuEtiketlerViewModel
   {
       Konus = context.Konu.ToList(),
       Etiketlers = context.Etiketler.ToList()
   }
   return View(viewModel);
}

View:

@model projectname.ViewModels.KonuEtiketlerViewModel // you must create a new class, preferably inside a ViewModels folder, called KonuEtiketlerViewModel
//you can access all your properties here....
SOfanatic
  • 5,523
  • 5
  • 36
  • 57
  • SOfanatic , is it also for entity ? – Soner Jun 17 '13 at 12:30
  • @SonerSevinc what do you mean? – SOfanatic Jun 17 '13 at 12:33
  • i am using entity framework for model. is there any difference for entity ? – Soner Jun 17 '13 at 12:34
  • Konus = context.Konu.ToList(); Etiketlers = context.Etiketler.ToList(); Konus and Etiketlers dont work why ? – Soner Jun 17 '13 at 12:42
  • what do you mean it doesn't work? are you getting an error, or it's not giving you the data you expect? – SOfanatic Jun 17 '13 at 12:47
  • 1-konus and etiketlers Their under line red color. Maybe i must write something var konus or var etiketlers ? Because it is not working. 2-İn my view @model IEnumerable Should i change this ? – Soner Jun 17 '13 at 12:51
  • @SonerSevinc see my edit, there should be a comma after `context.Konu.ToList(),` and nothing after `context.Etiketler.ToList()` . check answer for the view too. – SOfanatic Jun 17 '13 at 12:53
  • there is a mistake in the code, in the view should be: @model projectname.ViewModels.KonuEtiketlerViewModel – Vic Jun 17 '13 at 13:10