MVC- 计数记录数
MVC- Count Number of Records
编辑 我的观点是使用 Employer 模型。雇主和 JobPosting 具有 1:M 关系。我将分享更多上下文视图。
上下文:在我的应用程序中,我想向雇主显示申请 JobPosting 的申请人数。我目前编写的代码没有返回任何值。它没有抛出任何错误 - 但它也不起作用。我很确定问题出在我的控制器上,但我也会提供模型和视图。
控制器:
public ActionResult AppCount()
{
foreach (var app in db.JobPostings.ToList())
{
int id = app.JobPostingID;
int count= db.Applications.Where(a => a.JobPostingID == id).Count();
app.AppCount = count;
ViewBag.AppCount = count;
}
return View();
}
查看:
@model InTurn_Model.Employer
.
.
.
<h2>My Job Postings</h2>
<p>
@Html.ActionLink("Create New", "Create", "JobPostings", null, null)
</p>
<div id="employeeContainer"></div>
<table class="table table-striped">
<tr>
<th>Position</th>
<th>Job Type</th>
<th>Number of Applicatiosn</th>
<th></th>
</tr>
@foreach (var item in Model.JobPostings)
{
if (item.EmployerID == Model.EmployerID)
{
<tr>
<td>
@Html.DisplayFor(model => item.Position)
</td>
<td>
@Html.DisplayFor(model => item.JobType)
</td>
<td>@ViewBag.AppCount</td>
<td>@Html.ActionLink("Details", "Details", "JobPostings", new { id = item.JobPostingID }, null) </td>
</tr>
}
}
</table>
型号:
[MetadataType(typeof(JobPostingMetaData))]
public partial class JobPosting
{
public int AppCount { get; set; }
private sealed class JobPostingMetaData
{
[Display(Name = "Job Posting ID")]
public int JobPostingID { get; set; }
[Display(Name = "Employer ID")]
public int EmployerID { get; set; }
[Display(Name = "Description")]
public string Desc { get; set; }
[Display(Name = "Job Type")]
public JobType JobType { get; set; }
[Display(Name = "Employment Type")]
public TimeType TimeType { get; set; }
[DataType(DataType.Currency)]
public decimal Wage { get; set; }
}
}
我发现有两个问题。
首先,您没有将 Model
从控制器传递到视图。但是,您正在迭代 Model.JobPostings
。它是空的。
其次,您在循环中分配 ViewBag.AppCount
。因此,除最后一个值外,所有值都将丢失。但是如果你解决了第一个问题(使用 Model
而不是 ViewBag
)——第二个问题可能会自行消失。
您需要在视图中使用@model指定模型:
@model YourNameSpace.JobPosting
然后return那个模型到视图:
public ActionResult AppCount()
{
foreach (var app in db.JobPostings.ToList())
{
int id = app.JobPostingID;
int count= db.Applications.Where(a => a.JobPostingID == id).Count();
app.AppCount = count;
ViewBag.AppCount = count;
}
return View(app);
}
这将使模型中的值可用于视图。无需使用 ViewBag,因为 AppCount 是模型的一部分。
我想多了。我只需要从 JobPosting 模型设置它,然后其余的工作,我根本不需要遍历控制器。
public int AppCount => Applications.Count;
编辑 我的观点是使用 Employer 模型。雇主和 JobPosting 具有 1:M 关系。我将分享更多上下文视图。
上下文:在我的应用程序中,我想向雇主显示申请 JobPosting 的申请人数。我目前编写的代码没有返回任何值。它没有抛出任何错误 - 但它也不起作用。我很确定问题出在我的控制器上,但我也会提供模型和视图。
控制器:
public ActionResult AppCount()
{
foreach (var app in db.JobPostings.ToList())
{
int id = app.JobPostingID;
int count= db.Applications.Where(a => a.JobPostingID == id).Count();
app.AppCount = count;
ViewBag.AppCount = count;
}
return View();
}
查看:
@model InTurn_Model.Employer
.
.
.
<h2>My Job Postings</h2>
<p>
@Html.ActionLink("Create New", "Create", "JobPostings", null, null)
</p>
<div id="employeeContainer"></div>
<table class="table table-striped">
<tr>
<th>Position</th>
<th>Job Type</th>
<th>Number of Applicatiosn</th>
<th></th>
</tr>
@foreach (var item in Model.JobPostings)
{
if (item.EmployerID == Model.EmployerID)
{
<tr>
<td>
@Html.DisplayFor(model => item.Position)
</td>
<td>
@Html.DisplayFor(model => item.JobType)
</td>
<td>@ViewBag.AppCount</td>
<td>@Html.ActionLink("Details", "Details", "JobPostings", new { id = item.JobPostingID }, null) </td>
</tr>
}
}
</table>
型号:
[MetadataType(typeof(JobPostingMetaData))]
public partial class JobPosting
{
public int AppCount { get; set; }
private sealed class JobPostingMetaData
{
[Display(Name = "Job Posting ID")]
public int JobPostingID { get; set; }
[Display(Name = "Employer ID")]
public int EmployerID { get; set; }
[Display(Name = "Description")]
public string Desc { get; set; }
[Display(Name = "Job Type")]
public JobType JobType { get; set; }
[Display(Name = "Employment Type")]
public TimeType TimeType { get; set; }
[DataType(DataType.Currency)]
public decimal Wage { get; set; }
}
}
我发现有两个问题。
首先,您没有将 Model
从控制器传递到视图。但是,您正在迭代 Model.JobPostings
。它是空的。
其次,您在循环中分配 ViewBag.AppCount
。因此,除最后一个值外,所有值都将丢失。但是如果你解决了第一个问题(使用 Model
而不是 ViewBag
)——第二个问题可能会自行消失。
您需要在视图中使用@model指定模型:
@model YourNameSpace.JobPosting
然后return那个模型到视图:
public ActionResult AppCount()
{
foreach (var app in db.JobPostings.ToList())
{
int id = app.JobPostingID;
int count= db.Applications.Where(a => a.JobPostingID == id).Count();
app.AppCount = count;
ViewBag.AppCount = count;
}
return View(app);
}
这将使模型中的值可用于视图。无需使用 ViewBag,因为 AppCount 是模型的一部分。
我想多了。我只需要从 JobPosting 模型设置它,然后其余的工作,我根本不需要遍历控制器。
public int AppCount => Applications.Count;