3

I'm trying to learn how to use the ASP.NET MVC 4 and Entity FrameWork 5, and I'm a bit confused by scaffolding for Drop Down Lists.

I have three classes:

public class ScopeType
{
    public int ScopeTypeId { get; set; }

    [Required]
    public string Type { get; set; }
}

public class ScopeManufacturer
{
    public int ScopeManufacturerId { get; set; }

    [Required]
    [Display(Name="Manufacturer Name")]
    public string Name { get; set; }
}

public class Scope
{
    public int ScopeId { get; set; }

    [Required]
    public ScopeManufacturer ScopeManufacturer { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public ScopeType ScopeType { get; set; }

    [Required]
    public int Aperture { get; set; }

    [Required]
    public int FocalLength { get; set; }
}

Essentially, the first two classes are just lists of values that I want to appear in the drop downs on the 'Scope' create/edit forms. It's a 1 to 1 relationship.

I build the solution, and then add scaffolded controllers and views. Unfortunately, for the 'Scope' controller and views, the ScopeType and ScopeManufacturer navigation properties are ignored; no drop down lists are generated.

I then found on Google examples where people describe specifying the relationship between items by creating properties of integers, with the same name as the Id on the related thing. Therefore, I deleted the controllers and views, and tried again with:

public class Scope
{
    public int ScopeId { get; set; }

    [Required]
    public int ScopeManufacturerId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public int ScopeTypeId { get; set; }

    [Required]
    public int Aperture { get; set; }

    [Required]
    public int FocalLength { get; set; }
}

This still didn't scaffold drop down lists for me - rather, it gave me 2 extra fields for me to type integers into.

What am I doing wrong, or am I mistaken in believing that the scaffolding in MVC 4 will generate drop down lists for 1 to 1 relationships like that?

Andy Burns
  • 917
  • 4
  • 8
  • http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/examining-how-aspnet-mvc-scaffolds-the-dropdownlist-helper – Robert Harvey Jul 23 '14 at 16:59
  • Yes, I've already read that tutorial, but it doesn't make it any clearer why my scaffolding doesn't work. The source code for the tutorial is no longer available, and I do see examples of different ways of doing this being show. – Andy Burns Jul 24 '14 at 08:17

0 Answers0