无法在foreach循环中获取项的值

时间:2012-02-09 10:04:37

标签: asp.net-mvc asp.net-mvc-3 razor

我无法获取从控制器发送的视图中的值。

我尝试了两种方法,但我不能让它们出现在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。我再次尝试了几种方式,但没有一种方法有效。

我想将item作为标签的文本。我重申,当我在if,for或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;
        }
    }
}

1 个答案:

答案 0 :(得分: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>
相关问题