Domanda

I have 3 entity classes; Team, TeamContact and TeamAddress.

Relationships

-Team one-to-one TeamContact
-TeamContact one-to-on TeamAddress

Create Method

[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);
    }

My problem is that when i try scaffolding a create view, only labels and fields of entity Team are scaffolding. The view is being strongly typed agains team entity, my guess is that this is where the source of my problem is. See below.

Create View

@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>
}

Team

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

TeamContact

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

TeamAddress

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

What can i do to get passed this?

È stato utile?

Soluzione

You can use an EditorTemplate in your view that will render out content within the context of the Team object, so that the model binder will work when you post your form

1) In the folder where your view currently is, create a sub-folder called 'EditorTemplates'

2) Create a new partial view in this folder, name it the same as the model you are trying to scaffold, e.g. TeamContact

3) Write your HTML in the new view for the TeamContact model

4) In your current view for Team, you can now do this...

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

MVC will recognise that you have a custom template for this object type and will use your partial view to render the page.

When you look at the markup in the HTML page, you will see it renders controls as

Team.TeamContact.EmailAddress

...giving the TeamContact a context against the Team object.

Editor templates are excellent for this sort of thing, particularly if you have a collection of child objects - MVC will correctly index all of your items in the view

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top