我有一个应用程序asp.net mvc。在控制器中我有这个:
public ActionResult Index()
{
Upload.Models.ClientModels model1 = new Models.ClientModels();
ArrayList client = model1.Client_List();
Upload.Models.AkeoModels model2 = new Models.AkeoModels();
ArrayList akeo = model2.Akeo_List();
ArrayList model = new ArrayList();
model.Add(client);
model.Add(akeo);
return View(model);
}
我将两个连接列表作为模型传递给视图索引:
@{
ViewBag.Title = "Bienvenue";
int i = 0;
}
<hgroup class="title">
<h1 style="color:darkcyan">Liste des clients</h1>
</hgroup>
<section >
<form>
<table style="margin-top: 50px;">
<tr ><td ></td>
<td ><center><b>Login</b></center></td>
<td><center><b>Email</b></center></td>
<td><center><b>Password</b></center></td>
<td><center><b>Name</b></center></td>
</tr>
@{
Upload.Models.ClientModels client = null;
int j = 0;
while( j != -1)
{
try
{
client = Model[j];
<tr>
<td width=10px>
@if (i == 0)
{
<input type="radio"
name="color" checked >
}
else
{<input type="radio" name="color" >
}
</td>
<td>
<a type="text"
name="color" >@client.Login</a>
</td>
<td>
<a type="text"
name="color" >@client.Mail</a>
</td>
<td>
<a type="text"
name="color" >@client.Password</a>
</td>
<td>
<a type="text"
name="color" >@client.Name</a>
</td>
</tr>
i++;
j++;
}
catch {j = -1;}
}
i = 0;
}
</table>
</form>
</section>
<section style="margin-top: 30px">
<a type="button" style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;margin-left:150px" href="@Url.Action("Delete_client", "Admin")">Supprimer</a>
<a type="button" style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;" href="@Url.Action("Edit_client", "Admin",new { Id = 1 })">Editer</a>
<br /> <br />
<a href="@Url.Action("Create","Client")" style="color:blue; margin-top : 30px;margin-left:150px">Créer un nouveau compte</a>
</section>
<br />
<section>
<hgroup class="title">
<h1 style="color:darkcyan">Liste des akeos</h1>
</hgroup>
<section >
<form>
<table style="margin-top: 50px;">
<tr ><td ></td>
<td ><center><b>Login</b></center></td>
<td><center><b>Email</b></center></td>
<td><center><b>Password</b></center></td>
<td><center><b>Name</b></center></td>
</tr>
@{
Upload.Models.AkeoModels akeo = null;
int k = 0;
while( k < Model.Count)
{
try
{
akeo = Model[k];
<tr>
<td width=10px>
@if (i == 0){
<input type="radio"
name="color" checked >
}
else{<input type="radio" name="color" >
}
</td>
<td>
<a type="text"
name="color" >@akeo.Login</a>
</td>
<td>
<a type="text"
name="color" >@akeo.Mail</a>
</td>
<td>
<a type="text"
name="color" >@akeo.Password</a>
</td>
<td>
<a type="text"
name="color" >@akeo.Name</a>
</td>
</tr>
i++;
k++;
}
catch {k++;}
}
}
</table>
</form>
</section>
<section style="margin-top: 30px">
<a type="button" style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;margin-left:150px" href="@Url.Action("Delete_akeo", "Admin",new { Id = 1})">Supprimer</a>
<a type="button" style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;" href="@Url.Action("Edit_akeo", "Admin",new { Id = 1 })">Editer</a>
<br /> <br /> <br />
<a href="@Url.Action("Create","Akeo")" style="color:blue; margin-top : 30px;margin-left:150px">Créer un nouveau compte</a>
</section>
</section>
问题是:列表的值未显示。在视图索引中,仅显示表格下方的标签和按钮,但不显示模型的内容。我调试程序并且模型不为空,它包含两个元素。
答案 0 :(得分:5)
您不应该使用ArrayList
。您应该创建一个包含两个模型的属性的自定义类型。
例如:
public class CustomModel
{
public int PropertyOne { get; set; }
public string PropertyTwo { get; set; }
}
或者您也可以:
public class CustomModel2
{
public List<Models.ClientModels> ClientModels { get; set; }
public List<Models.Akeo_List> AkeoModels { get; set; }
}
<强>控制器:强>
public ActionResult Index()
{
var model = new List<CustomModel>();
model.Add(new CustomModel() { });
return View(model);
}
查看强>
@Model List<CustomModel>
foreach(var cm in @Model) {
cm.PropertyOne
}
如果您使用的是CustomModel2
,您可以执行以下操作迭代所有客户端:
foreach (var client in @Model.ClientModels) {
@client.ClientName
}
答案 1 :(得分:0)
如果你尝试使用@ Html.EditorFor(),你正在做MVC的M和V位有点奇怪吗?至于您在使用代码时遇到的问题。
您尚未加入
视图顶部的@model decleration。
编辑:对不起,我刚刚读了你的代码,这个位不正确。您将客户端设置为null然后循环它。
Upload.Models.ClientModels client = null;
id建议阅读一些Darins帖子https://stackoverflow.com/users/29407/darin-dimitrov他有关于模型和视图构建的优秀MVC帖子