Search code examples
asp.net-mvcentity-frameworkasp.net-mvc-4scaffolding

MVC 4 - Scaffolding - Why are my 1:1 and 1:M properties skipped?


I'm very new with MVC 4 and I have the following class:

public class BicycleSellerListing
{
    public int BicycleSellerListingId { get; set; }
    public UserProfile UserProfile { get; set; }
    public System.DateTimeOffset ListingDate { get; set; }
    public double ListingPrice { get; set; }
    public string BicycleModel { get; set; }
    public string Color { get; set; }
    public string Comments { get; set; }
    public BicycleManufacturer BicycleManfacturer { get; set; }
    public BicycleType BicycleType { get; set; }
    public BicycleFrameSize BicycleFrameSize { get; set; }
    public DateTime ModelYear { get; set; }
    public ICollection<BicycleAttribute> BicycleAttributeList { get; set; }
}

When I created a new Controller for this class, I selected the MVC controller with read/write actions and views, using Entity Framework scaffolding option. In the Create.cshtml, it skipped creating editors for all my 1:1 and 1:M properties (BicycleManufacturer, BicycleType etc.). Ideally I would have liked VS to create drop-down list editors for these properties.

Is it possible to have editors created for these properties, or do I need to do it manually?


Solution

  • Complex objects will need some help. Thankfully, this isn't difficult to do, and the MVC Framework is quite customizable in this regard.

    The first thing you'll want to do is to create views to handle editing and display. These go in your Editor and Display template folders under Views\Shared.

    https://github.com/MisterJames/BootstrappingMvc/tree/master/BootstrappingMvc/Views/Shared

    Next, you'll want to decorate your properties on your view models with UIHint to tell MVC what views to use.

    https://github.com/MisterJames/BootstrappingMvc/blob/master/BootstrappingMvc/Models/MoviePass.cs

    This will help take care of your 1:1. You'll want to look into something like MvcScaffolding to handle your list items, it does a fairly good job

    There is a full walkthrough here:

    http://jameschambers.com/2012/07/bootstrapping-mvc-say-no-to-checkboxes/

    Cheers.