我正在MVC中制作一个类似于Change.org的Web应用程序,用户可以在其中创建其他人签名的原因。在我的原因索引视图中,我试图显示已签署原因的人员列表。我可以使用“列表中的计数”来显示签名的数量,但是我正在努力访问列表中的项目以显示它们。任何帮助,将不胜感激。
我的模型像这样分为原因,签名和类别...
原因:
public class Cause
{
[Key]
[StringLength(100)]
[DisplayName("Cause Title")]
public string CauseTitle { get; set; }
[Required]
[StringLength(500)]
public string Description { get; set; }
[Required]
public string UserEmail { get; set; }
[Required]
[DisplayName("Date Posted")]
public DateTime DatePosted { get; set; }
public string ImageURL { get; set; }
[DisplayName("Catagory")]
public string CatagoryTitle { get; set; }
[ForeignKey("CatagoryTitle")]
public Catagory Catagory { get; set; }
[DisplayName("Signatures Target")]
public int TargetSignatures { get; set; }
public virtual List<Signature> Signatures { get; set; }
}
类别:
public class Catagory
{
[DisplayName("Catagory Title")]
[Key]
public string CatagoryTitle { get; set; }
[DisplayName("Catagory Description")]
public string CatagoryDescription { get; set; }
public virtual List<Cause> Causes { get; set; }
}
签名:
public class Signature
{
[Key]
public int SignatureId { get; set; }
[Required]
public string UserEmail { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string CauseTitle { get; set; }
[ForeignKey("CauseTitle")]
public virtual Cause Cause { get; set; }
}
我的原因索引控制器是:
public class CausesController : Controller
{
private CauseContext db = new CauseContext();
// GET: Causes
public ActionResult Index()
{
var causes = db.Causes.Include(c => c.Catagory);
return View(causes.ToList());
}
我的索引视图是:
@model IEnumerable<SoWokeWebApp.Models.Cause.Cause>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var item in Model)
{
<div class="jumbotron">
<img src="@item.ImageURL" alt="Alternate Text" class="img-fluid" />
<h1>@Html.DisplayFor(model => item.CauseTitle)</h1>
<p>@Html.DisplayFor(model => item.Description)</p>
<p>@item.Signatures.Count</p>
@foreach (var Signature in item.Signatures)
{
<p>Signature.</p>
}
<p><a class="btn btn-primary btn-lg" href="#" role="button">@Html.ActionLink("Sign Cause", "Create", "Signatures")</a></p>
</div>
答案 0 :(得分:0)
您是否调试了代码并检查了签名数据是否到来?我认为您必须在索引方法模型中包括签名列表。