【问题标题】:how to get one item of the list in a foreach ViewBag mvc 5如何在foreach ViewBag mvc 5中获取列表中的一项
【发布时间】:2019-08-30 18:18:26
【问题描述】:

我想让所有学生在 foreach 中拥有 ContractStatus == "Pending" 并创建一个链接以编辑学生

<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”不包含“contractStatus”的定义

【问题讨论】:

标签: c# razor viewbag actionlink asp.net-mvc-5.2


【解决方案1】:

您对变量名和重新分配的选择在这里伤害了您。因此,您的类型和遇到的错误会混杂在一起。

我建议不要使用三元条件(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 studentCohort,而不是var studentCohort

【讨论】:

  • 我对你的代码做了一些修改,但是非常感谢
【解决方案2】:

改变

(@studentCohort.contractStatus)

(@studentCohort.ContractStatus)

在你的 Razor 视图中。

【讨论】:

  • 我已经尝试过了,是的,它给了我 ContractStatus 但是当状态为“Pending”时,不是我想要的链接
  • (@studentCohort.contractStatus)更改为(@contractStatus )
  • "(XXXX.Models.StudentCohortViewModel)" 我明白了
  • 即使是待定的?对于所有学生,你看到了吗?
  • var contractStatus = studentCohort.ContractStatus == "Pending" ? link : studentCohort; 这是您代码中的问题陈述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
相关资源
最近更新 更多