0

我在数据库中有 Konu 和 Etiketler 表。

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

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

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

}

在 Konu 控制器的视图中,

我想调用 Etiketler 表及其 ID。

我已经使用 Konu 模型但我想在 1 个视图中使用 Etiketler 模型。

我如何在 1 个视图页面中调用 2 个表(模型)?

4

1 回答 1

0

您将需要创建一个 ViewModel:

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

在你的控制器中:

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

看法:

@model projectname.ViewModels.KonuEtiketlerViewModel // you must create a new class, preferably inside a ViewModels folder, called KonuEtiketlerViewModel
//you can access all your properties here....
于 2013-06-17T12:29:47.537 回答