我正在开发一个小应用程序来存储和显示费用。
该应用程序非常简单,会员收费很多。费用也可以有FeeTypes。
我遇到通过剃刀语法显示相关记录的问题。
我需要根据费用表中的FeeTypesId显示FeeTypeName,但我似乎正在撞墙。外键是FeeTypesId,对应于FeeTypes上的PK Id。
我是否认为这一切都错了?我不想查询每条记录,因为费用已经存储了Id就好了。 机型:
public class Members
{
[Key]
public int MemberNo { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Middle Name")]
public string MiddleName { get; set; }
[Display(Name = "Surname")]
public string Surname { get; set; }
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
[Display(Name = "Primary Contact Email")]
public string MemberPrimaryContactEmail { get; set; }
[Display(Name = "Member Status")]
public MemberStatus MemberStatus { get; set; }
[Display(Name = "Member Fees")]
public virtual ICollection<Fees> Fees { get; set; }
[Display(Name = "Member FUM")]
public float? MemberFUM { get; set; }
public string MemberWCName { get; set; }
public string MemberAdviceOSName { get; set; }
public string MemberRevexName { get; set; }
[Display(Name = "Accounting Firm")]
public virtual Accountants AccountantNo { get; set; }
[Display(Name = "Adviser Name")]
public virtual Adviser Adviser { get; set; }
[Display(Name = "Head Company")]
public virtual HeadCompany HeadCompany { get; set; }
public DateTime? Created_Date { get; set; }
public string Created_By { get; set; }
public DateTime? Modified_Date { get; set; }
public string Modified_By { get; set; }
}
public class Fees
{
[Key]
public int FeeNo { get; set; }
public FeeTypes FeeType { get; set; }
[Display(Name = "Fee Type")]
public int FeeTypesId { get; set; }
[Display(Name = "Frequency of Fee")]
public FeeOccurence FeeOccurence { get; set; }
[Display(Name = "Fee Amount")]
public float FeeAmount { get; set; }
[Display(Name = "Fee Status")]
public FeeStatus FeeStatus { get; set; }
[Display(Name = "Date received (for billing)")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? FeeDate { get; set; }
public virtual Members Members { get; set; }
public int MemberNo { get; set; }
public DateTime? Created_Date { get; set; }
public string Created_By { get; set; }
public DateTime? Modified_Date { get; set; }
public string Modified_By { get; set; }
}
public class FeeTypes
{
public int Id { get; set; }
[Display(Name = "Fee Name")]
public string FeeTypeName { get; set; }
}
控制器 - 我试图显示FeeTypeName
的位置 public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Members members = db.Members.Find(id);
ViewBag.Fees = db.Fees.Where(x => x.Members.MemberNo.Equals(x.MemberNo));
ViewBag.FeeTypesId = new SelectList(db.FeeTypes, "Id", "FeeTypeName");
if (members == null)
{
return HttpNotFound();
}
return View(members);
}
视图
@model AccPortal.Models.Members
@{
ViewBag.Title = "Details";
}
@{ int i = 0; }
@foreach (var item in Model.Fees)
{
<tr data-id="@i">
@Html.HiddenFor(model => model.Fees.ElementAt(i).FeeNo, new { @class = "md-fee-value", name = "md-fee-id", data_type = "testid", id = "testid" })
<td class="md-fee-value" data-type="date">@String.Format("{0:d}", item.FeeDate)</td>
<td class="md-fee-value" data-type="type">@item.FeeOccurence</td>
//NEED TO DISPLAY FeeTypeName here instead of FeeTypesId
<td class="md-fee-value" data-type="type">@item.FeeTypesId</td>
<td class="md-fee-value" data-type="status">@item.FeeStatus</td>
<td class="md-fee-value" data-type="amount">@item.FeeAmount</td>
<td>
<button type="button" class="md-button md-button--edit" title="edit">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</button>
<button type="button" class="md-button md-button--remove" title="remove">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
</td>
</tr>
i++;
}
答案 0 :(得分:0)
通过将属性标记为Fees
来修改virtual
模型:
public virtual FeeTypes FeeType { get; set; }
检索数据时包括FeeType
:
ViewBag.Fees = db.Fees
.Include(x => x.FeeTypes)
.Where(x => x.Members.MemberNo.Equals(x.MemberNo));
显示它:
<td class="md-fee-value" data-type="type">@item.FeeTypes.Name</td>