C# 如何在foreach ViewBag mvc 5中获取列表中的一项

C# 如何在foreach ViewBag mvc 5中获取列表中的一项,c#,razor,viewbag,actionlink,asp.net-mvc-5.2,C#,Razor,Viewbag,Actionlink,Asp.net Mvc 5.2,我想让所有学生在foreach中都有ContractStatus==挂起,并创建一个链接来编辑学生 <div class="row"> @{ foreach (var studentCohort in ViewBag.CohortSubscriptionId) { <div class="col-md-5"> @{ var link = Html.ActionLi

我想让所有学生在foreach中都有ContractStatus==挂起,并创建一个链接来编辑学生

<div class="row">
     @{ foreach (var studentCohort in ViewBag.CohortSubscriptionId)
         {
         <div class="col-md-5">
              @{

                 var link = Html.ActionLink((string) studentCohort.ContractStatus, "Edit", "CohortSubscriptions", new { id = (object) studentCohort.ContractStatus }, new { @class ="form-control"});

                 var contractStatus = studentCohort.ContractStatus == "Pending" ? link : studentCohort;
               }

               @studentCohort.FullName  (@studentCohort.contractStatus)
          </div>
          }
      }
</div>

public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Cohorts cohorts = db.Cohorts.Find(id);
            if (cohorts == null)
            {
                return HttpNotFound();
            }

            var studentByCohort = db.Enrolled_Students.Where(x => x.CohortId == id).Select(x => new StudentCohortViewModel
            {
                CohortId = x.CohortId,
                FirstName = x.FirstName,
                LastName = x.LastName,
                ContractStatus = x.ContractStatus

            }).Distinct().ToList();
            ViewBag.CohortSubscriptionId = studentByCohort;

            return View(cohorts);
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:CodeboxxSchoolPortal.Models.StudentCohortViewModel“未包含“合同状态”的定义

@学生队列状态

@学生队列状态


在你的剃刀看来。

你在变量名和重新分配方面的选择正在伤害你。因此,你会把你的类型和你所经历的错误混为一谈

而不是三元条件c?a:b,我建议使用简单易读写的基本if/else

<div class="row">
    @foreach(StudentCohortViewModel studentCohort in ViewBag.CohortSubscriptionId)
    {
        if (studentCohort.ContractStatus == "Pending")
        {
            <text>
                @studentCohort.FullName (@Html.ActionLink(studentCohort.ContractStatus,
                                               "Edit",
                                               "CohortSubscriptions",
                                               new { id = student.ContractStatus },
                                               new { @class = "form-control" }))
            </text>
        }
        else
        {
            <text>
                @studentCohort.FullName (@studentCohort.ContractStatus)
            </text>
        }
    }
</div>

要帮助识别正确的类型,请使用更明确的StudentCohortViewModel StudentCourt,而不是var StudentCourt。

我们需要更多代码来帮助您。StudentCohortViewModel的代码将非常有用。done@PaulCarltonWhy不使用视图模型吗?我已经试过了,是的,它给了我合同状态,但当状态为“待定”时,它不是一个像我想更改@studentcourt.ContractStatus到@ContractStatus XXXX.Models.studentcourtViewModel这样的链接,即使是对于待定的?对于您看到的所有学生?var contractStatus=StudentCoquent.contractStatus==Pending?链接:学生队列;这是你的代码中的问题陈述。我对你的代码做了一些更改,但效果很好,非常感谢