【发布时间】: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”的定义
【问题讨论】:
-
我们需要更多代码来帮助您。
StudentCohortViewModel的代码会有很大帮助。 -
完成@PaulCarlton
-
为什么不改用
View Model呢? stackoverflow.com/questions/32906975/…
标签: c# razor viewbag actionlink asp.net-mvc-5.2