如何在MVC3中从控制器返回一个数组?

时间:2012-09-05 14:58:25

标签: asp.net-mvc-3

大家好我正在尝试从控制器返回一个数组来查看。这是我的控制器:

public ActionResult Index()

        {
            TahakkukServicesClient client = new TahakkukServicesClient();
            client.ClientCredentials.UserName.UserName = "service_test";
            client.ClientCredentials.UserName.Password = "..";
            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            MakbuzList[] liste = client.GetMakbuzListe(2);
            return View(liste);
        }

    }
}

那么我如何在视图中显示数组的值?

2 个答案:

答案 0 :(得分:1)

MakbuzList[]“是”IEnumerable<MakbuzList>(注意:现在,通用列表/集合通常比数组更受欢迎)

因此,您可以强烈地键入您的视图

@model IEnumerable<MakbuzList>

并显示如下的值

@if (Model.Any()) {
  foreach (var makbuz in Model) {
     @Html.DisplayFor(x => makbuz.Property1)
  }
}

答案 1 :(得分:0)

假设视图已配置

@model IList<MakbuzList>

然后你可以简单地做

@for (var i = 0; i < Model.Count; ++i) {
    @Html.DisplayFor(m => m[i]);
}