2013-05-01 17 views
5

Veritabanımdaki iki farklı model için bir liste şeklinde bir MVC4 projesindeki bir görünüme veri göndermem gerekiyor. BöyleMVC4 ViewBag veya ViewModel veya?

şey:

Kontrolör:

public ActionResult Index() 
{ 
    Entities db = new Entities(); 

    ViewData["Cats"] = db.Cats.toList(); 
    ViewData["Dogs"] = db.Dogs.toList(); 

    return View(); 
} 

Görünüm:

@* LIST ONE *@ 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.ListOneColOne) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ListOneColTwo) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ListOneColThree) 
     </th> 
    </tr> 

@foreach (var item in @ViewData["Cats"]) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ListOneColOne) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ListOneColTwo) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ListOneColThree) 
     </td> 
    </tr> 


@* LIST TWO *@ 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.ListTwoColOne) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ListTwoColTwo) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ListTwoColThree) 
     </th> 
    </tr> 

@foreach (var item in @ViewData["Dogs"]) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ListTwoColOne) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ListTwoColTwo) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ListTwoColThree) 
     </td> 
    </tr> 

Görünüm iki listenin bir listesini görüntülemek için model başına.

Bunun için en etkili yolun ne olduğundan emin değilim?

Viewmodel?

Viewdata/Viewbag?

Başka bir şey?

(Lütfen hiçbir üçüncü parti önerileri)

GÜNCELLEME: Daha ben bir saatten fazla teşebbüs ettik

şimdi olmadan herhangi bir şans bir List<T> ViewModel düşündüren cevabı uygulamaktır. Bu benim ViewModel şöyle görünür olmasından kaynaklanmaktadır inanıyoruz:

public class GalleryViewModel 
{ 
    public Cat cat { get; set; } 
    public Dog dog { get; set; } 
} 

cevap

7

deneyin sorununuzu ve hedefinizi açıklamak, bu yüzden yapmaya çalıştığımız şey (özellikle) biliyorum.

Bunu iki listeye sahip olduğunuz ve bunları bir görünüme göndermek istediğiniz anlamına gelir. Bunu yapmanın bir yolu, iki listeyi bir modele koymak ve modeli görüntüye göndermektir, ancak zaten iki modelinizin olduğunu belirtmişsinizdir, bu yüzden bu varsayımla gideceğim.

Kontrolör

public ActionResult Index() 
{ 
    ModelA myModelA = new ModelA(); 
    ModelB myModelB = new ModelB(); 

    IndexViewModel viewModel = new IndexViewModel(); 

    viewModel.myModelA = myModelA; 
    viewModel.myModelB = myModelB; 

    return View(viewModel); 
} 

Görünüm Modeli

public class IndexViewModel 
{ 
    public ModelA myModelA { get; set; } 
    public ModelB myModelB { get; set; } 
} 

Modeli

public class ModelA 
{ 
    public List<String> ListA { get; set; } 
} 

public class ModelB 
{ 
    public List<String> ListB { get; set; } 
} 

Görünüm

@model IndexViewModel 

@foreach (String item in model.myModelA) 
{ 
    @item.ToString() 
} 

(Üzgünüm benim C# paslı ise) Ben bu işe ama onu anlamaya gibi olamaz almaya çalışıyorum @row

+0

teşekkürler. Görünen o ki, ViewModel'im 'List ' listesinden değil, gerçek model türü ör., 'Public ListA listA {get; set; } '. İlk yazımı bunu yansıtmak için güncelleyeceğim. –

+0

Bir süre aldı ama anladım! Teşekkürler Rowan. –