2012-02-09 83 views
0

我無法從控制器發送的視圖中獲取值。無法獲得foreach循環中項目的值

我試過兩種方法,但我不能讓它們出現在div中。我必須在表格或標籤中顯示值。

控制器:

List<string> tsList = new List<string>(); 
ts.tarih = ogrenci.Tarih; 
tsList.Add(ts.tarih); 
tsList.Add(ogrenci.TaksitSayisi); 
tsList.Add((36000/Convert.ToInt32(ogrenci.TaksitSayisi)).ToString()); 

string odeme=(36000/Convert.ToInt32(ogrenci.TaksitSayisi)).ToString(); 
List<TaksitSaysi> lstTaksit = new List<TaksitSaysi>(); 
lstTaksit.Add(new TaksitSaysi() 
{ 
    taksitSayisi = ogrenci.TaksitSayisi, 
    tarih = ogrenci.Tarih, tutar = odeme 
}); 
return View("Index",lstTaksit); 

我第一次嘗試tsList但不能顯示在標籤或DIV的項目。

現在我試試lstTaksit。我再次嘗試了幾種方法,但沒有一個能夠工作。

我想要項目是標籤的文本。當我在if,for,or foreach等中編寫代碼時,我沒有將其展示出來。例如我創建了一個DIV,寫在它的一些文字和它並不頁面上顯示

我的看法是:

@model IEnumerable<TaksitSaysi> 
@if (Model != null) 
{ 
    foreach (var item in Model) 
    { 
     if (item != null) 
     { 

      **<div>deneme</div>** 
      <table id="Table" > 
      @for (int i=0;i<Convert.ToInt32(item.taksitSayisi) ;i++) 
      { 
       <text> <tr><td> @item.tutar </td></tr></text> 
      } 
      </table> 
      break; 
     } 
    } 
} 

回答

0

在你的模型,你將要創建自定義的數據類型。

下面是一個簡單的例子,可以幫助你的想法:

型號:

public class MyType 
{ 
    public string Item1 { get; set; } 
    public string Item2 { get; set; } 
    public string Item3 { get; set; } 
} 

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var myList = new List<MyType>(); 

     var customType1 = new MyType(); 
     customType1.Item1 = "Item 1a"; 
     customType1.Item2 = "Item 2a"; 
     customType1.Item3 = "Item 3a"; 
     myList.Add(customType1); 

     var customType2 = new MyType(); 
     customType2.Item1 = "Item 1b"; 
     customType2.Item2 = "Item 2b"; 
     customType2.Item3 = "Item 3b"; 
     myList.Add(customType2); 

     return View(myList); 
    } 
} 

查看:

@model IEnumerable<MvcApplication1.Models.MyType> 
<table> 
@foreach (var item in Model) { 
    <tr> 
     <td> 
      @item.Item1; 
      @item.Item2; 
      @item.Item3; 
     </td> 
    </tr> 
} 
</table>