Bind drop down item to object

Feb 20, 2011 at 10:26 AM

Hi I’ve been using your scaffold tool. Really nice peace of work.

I'm having one issue, when I started I used convention of ID to define foriegn keys in entity framework code first e.g.public int TeamId {get;set;}Which scaffold the drop downs on create and edit a treat.  When you move to using the object ref e.g. public virtual Team Team {get;set; }It now scaffolds the display for index and details but you lose the drop down functionality on create and edit screens.

Feb 22, 2011 at 5:45 PM

Hi feanz,

     I have too this problem when i remove the public property TeamId, eg. It´s an Issue that is good to solve.

So, above follow the example my domain structure:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc3SampleDDLFor.Models
{
    public class Player
    {
        public int PlayerId { get; set; }
        public string Name { get; set; }
        //public int TeamId { get; set; }

        public virtual Team Team { get; set; } // This is new
    }
}

...and:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace Mvc3SampleDDLFor.Models
{
    public class Team
    {
        public int TeamId { get; set; }
        [Required]
        public string Name { get; set; }
        public string City { get; set; }
        public DateTime Founded { get; set; }

        public virtual ICollection<Player> Players { get; set; } // This is new
    }
}

ie i needn´t declare TeamId as Player´s Property, to Bind DropDown work; only with public virtual Team Team { get; set; }