0

我有 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; }
   }
 }

我该怎么做才能通过这个?

4

1 回答 1

1

您可以在视图中使用 EditorTemplate,它将在 Team 对象的上下文中呈现内容,以便模型绑定器在您发布表单时工作

1) 在当前视图所在的文件夹中,创建一个名为“EditorTemplates”的子文件夹

2) 在此文件夹中创建一个新的局部视图,将其命名为与您要搭建的模型相同的名称,例如 TeamContact

3) 在 TeamContact 模型的新视图中编写 HTML

4) 在您当前的团队视图中,您现在可以执行此操作...

@Html.EditorFor(model => model.TeamContact)

MVC 将识别出您有一个用于此对象类型的自定义模板,并将使用您的局部视图来呈现页面。

当您查看 HTML 页面中的标记时,您会看到它将控件呈现为

Team.TeamContact.EmailAddress

...为 TeamContact 提供针对 Team 对象的上下文。

编辑器模板非常适合这类事情,特别是如果您有一组子对象 - MVC 将正确索引视图中的所有项目

One additional thing, you should probably consider splitting out your 'entity domain' objects from what you are trying to render in the view. View Models are used for this, and allow you to keep your domain\data objects out of the presentation layer.

于 2012-11-27T17:23:55.243 回答