我一直试图通过使用@foreach()来显示记录及其相关记录,但是每次我不断收到错误消息“ foreach语句不能对类型变量进行操作。”。 VS解决方案基于“代码优先”模型,并且此操作仅在单个类上。 对此的任何帮助和指导都将有所帮助:
模型:
public class SingleView //USSD
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Client { get; set; }
public string PolicyNo { get; set; }
public Nullable<short> PolicyType { get; set; }
public string InsurerName { get; set; }
public Nullable<System.DateTime> RenewalDate { get; set; }
public string Status { get; set; }
public string Telephone { get; set; }
}
控制器
public actionResult Details(Int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SingleView singleView = db.SingleViews.Find(id);
if (singleView == null)
{
return HttpNotFound();
}
return View(singleView);
}
视图:
@model MinetSingleView.Models.SingleView
@{
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(model => model.Client)</td>
<td> @Html.DisplayFor(model => model.InsurerName)</td>
<td>Product Type</td>
<td>@Html.DisplayFor(model => model.PolicyType)</td>
<td>@Html.DisplayFor(model => model.RenewalDate)</td>
<td>@Html.DisplayFor(model => model.PolicyNo)</td>
<td>@Html.DisplayFor(model => model.Telephone)</td>
<td>@Html.DisplayFor(model => model.Status)</td>
</tr>
}
}
“详细信息”部分使用自动递增的“ ID”作为id参数 例如本地主机:65129 /首页/详细信息/ 16