使用foreach迭代类似记录

我一直试图通过使用@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

lorndrifter 回答:使用foreach迭代类似记录

您的模型是一个单个对象,控制器的签名建议它应该仅返回一个对象的数据-它需要一个id Details(Int id)

您不需要foreach

请考虑以下代码段。

var c = new SomeClass();

foreach( var item in c ) 
{
   What to iterate through here? 
}

返回多个对象的方法如下所示。

public ActionResult Details(sring clientName )
{
    if (string.IsNUllOrEmpty(clientName))
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var svs = db.SingleViews.Where( sv => sv.Client == clientName).ToList();
    if (!svs.Any())
    {
        return HttpNotFound();
    }
    return View(svs);
}

此视图的Model将是一个集合,然后您可以使用foreach

,

控制器

  public ActionResult Details(Int id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        List<SingleView> singleView = db.SingleViews.count > 0 ?
                                 db.SingleViews.where(a => a.id == id).tolist() : null ;
        if (singleView == null)
        {
            return HttpNotFound();
        }
        return View(singleView);
    }

查看

    @model  List<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>
                    }
           }

,
  

“ foreach语句无法对类型变量进行操作。”

出现此错误是因为foreach语句只能在集合上工作。 您的SingleView不是集合。您可以通过实现GetEnumerator

使其变得可迭代

无论如何,最简单的方法就是将其转换为列表。

并将列表传递到视图

只需遵循 tymtams 示例。

并在您的视图中添加此行

 @model  List<MinetSingleView.Models.SingleView>
本文链接:https://www.f2er.com/3159803.html

大家都在问