我有 3 个实体类;团队、团队联系人和团队地址。
关系
-Team one-to-one TeamContact
-TeamContact one-to-on TeamAddress
创建方法
[HttpPost]
public ActionResult Create(Team model)
{
if (ModelState.IsValid)
{
new Team
{
Name = model.Name,
Division = model.Division,
Description = model.Description,
TeamContact = new TeamContact
{
EmailAddress = model.TeamContact.EmailAddress,
PhoneNumber = model.TeamContact.PhoneNumber,
TeamAddress = new TeamAddress
{
Box = model.TeamContact.TeamAddress.Box,
StreetName = model.TeamContact.TeamAddress.StreetName,
StreetNumber = model.TeamContact.TeamAddress.StreetNumber,
City = model.TeamContact.TeamAddress.City,
PostalCode = model.TeamContact.TeamAddress.PostalCode,
Province = model.TeamContact.TeamAddress.Province
}
}
};
_dataSource.Save();
}
return View(model);
}
我的问题是,当我尝试为创建视图搭建脚手架时,只有实体 Team 的标签和字段在搭建脚手架。该视图正在针对团队实体进行强类型化,我的猜测是这就是我的问题的根源所在。见下文。
创建视图
@model Soccer.Domain.Entities.Club.Team
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Team</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Division)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Division)
@Html.ValidationMessageFor(model => model.Division)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
团队
namespace Soccer.Domain.Entities.Club
{
public class Team
{
//Holds players into a specific teams
[HiddenInput(DisplayValue = false)]
public virtual int Id { get; set; }
[Display(Name = "Full Name:")]
public virtual string Name { get; set; }
//Current playing division
[Display(Name = "Division:")]
public virtual string Division { get; set; }
[Display(Name = "About the team:")]
[DataType(DataType.MultilineText)]
public virtual string Description { get; set; }
public virtual TeamContact TeamContact { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
}
团队联系
namespace Soccer.Domain.Entities.Club
{
public class TeamContact
{
[Key]
[ForeignKey("Team")]
[HiddenInput(DisplayValue = false)]
public virtual int TeamId { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Email:")]
public virtual string EmailAddress { get; set; }
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone:")]
public virtual string PhoneNumber { get; set; }
public virtual Team Team { get; set; }
public virtual TeamAddress TeamAddress { get; set; }
}
}
团队地址
namespace Soccer.Domain.Entities.Club
{
public class TeamAddress
{
//Team Mailing address
[Key]
[ForeignKey("TeamContact")]
[HiddenInput(DisplayValue = false)]
public virtual int TeamContactId { get; set; }
[Display(Name = "P.O.Box:")]
public virtual string Box { get; set; }
[Display(Name = "Street Name:")]
public virtual string StreetName { get; set; }
[Display(Name = "Street Number:")]
public virtual string StreetNumber { get; set; }
[Display(Name = "City:")]
public virtual string City { get; set; }
[Display(Name = "Postal Code:")]
public virtual string PostalCode { get; set; }
[Display(Name = "Province:")]
public virtual string Province { get; set; }
public virtual TeamContact TeamContact { get; set; }
}
}
我该怎么做才能通过这个?