我的控制器出了问题。我强烈输入的视图中没有显示我的表中的数据。但是当我return View(CanaClie0012.toList())
;我可以完美地看到我的数据。我没有得到任何错误。谁能告诉我我做错了什么。
我的表格模型:
public partial class CanaClie0012
{
public string Client00130012 { get; set; }
public string F1Pais00200012 { get; set; }
public string F1Cana02530012 { get; set; }
public string Direcc0012 { get; set; }
public Nullable<System.DateTime> TmStmp0012 { get; set; }
}
public partial class Clientes0013
{
public string Client0013 { get; set; }
public string Nombre0013 { get; set; }
public string F1Pais00200013 { get; set; }
}
我的自定义模型将两个表合并为:
public class ClientModel
{
public CanaClie0012 CanaClie0012 { get; set; }
public Clientes0013 Clientes0013 { get; set; }
}
我的控制器:
public ViewResult Index()
{
List<ClientModel> lm = new List<ClientModel>();
ClientModel vm = new ClientModel();
lm.Add(vm);
return View(lm);
}
我的观点:
@model IEnumerable<ContactManager.Models.ClientModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Client00130012
</th>
<th>
F1Pais00200012
</th>
<th>
F1Cana02530012
</th>
<th>
Direcc0012
</th>
<th>
TmStmp0012
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CanaClie0012.Client00130012)
</td>
<td>
@Html.DisplayFor(modelItem => item.CanaClie0012.F1Pais00200012)
</td>
<td>
@Html.DisplayFor(modelItem => item.CanaClie0012.F1Cana02530012)
</td>
<td>
@Html.DisplayFor(modelItem => item.CanaClie0012.Direcc0012)
</td>
<td>
@Html.DisplayFor(modelItem => item.CanaClie0012.TmStmp0012)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
答案 0 :(得分:1)
在您的控制器中,您只需实例化一个新的ClientModel对象并将其添加到列表中。这真的是你索引方法中的内容吗?如果是这样,属性将为null,问题在于您的控制器而不是您的视图。如果没有,你能发布你的索引方法实际上做了什么吗?此外,在您的ClientModel中,属性是CanaClie0012,那么CanaClie0012.toList()如何发挥作用?
我不确定CanaClie0012和Clientes0013是如何相关的,虽然我假设他们在ClientModel中将它们组合在一起。为了举例,我们可以通过在CanaClie0012中添加一个id字段和Clientes0013中的相应字段来修改它们,这样我们就可以加入这两个类。同样,我只是这样做来显示控制器的外观。所以现在我们有了以下内容。
public partial class CanaClie0012
{
public string Id {get; set; }
public string Client00130012 { get; set; }
public string F1Pais00200012 { get; set; }
public string F1Cana02530012 { get; set; }
public string Direcc0012 { get; set; }
public Nullable<System.DateTime> TmStmp0012 { get; set; }
}
public partial class Clientes0013
{
public string CanaClie0012Id {get; set; }
public string Client0013 { get; set; }
public string Nombre0013 { get; set; }
public string F1Pais00200013 { get; set; }
}
然后你的控制器看起来像。
public ViewResult Index()
{
using (var context = new MMProdatEntities())
{
var lm = from cc in context.CanaClie0012
join c in context.Clientes0013 on cc.Id equals c.CanaClie0012Id
select new ClientModel() {
CanaClie0012 = new CanaClie0012() {
Client00130012 = cc.Client00130012,
F1Pais00200012 = cc.F1Pais00200012,
F1Cana02530012 = cc.F1Cana02530012,
Direcc0012 = cc.Direcc0012
},
Clientes0013 = new Clientes0013() {
Client0013 = c.Client0013,
Nombre0013 = c.Nombre0013,
F1Pais00200013 = c.F1Pais00200013
}
};
return View(lm);
}
}
哎呀你甚至做了以下事情来确认问题与视图无关
public ViewResult Index()
{
List<ClientModel> lm = new List<ClientModel>();
ClientModel vm = new ClientModel()
{
CanaClie0012 = new CanaClie0012()
{
Client00130012 = "Client00130012 Data",
F1Pais00200012 = "F1Pais00200012 data",
F1Cana02530012 = "F1Cana02530012 data"
},
Clientes0013 = new Clientes0013()
{
Client0013 = "Client0013 data",
Nombre0013 = "Nombre0013 data",
F1Pais00200013 = "F1Pais00200013 data"
}
};
lm.Add(vm);
return View(lm);
}
答案 1 :(得分:0)
foreach (var item in Model)
{
@Html.LabelForModel(item.CanaClie0012.Client00130012)
}