Data Access has been discontinued. Please refer to this page for more information.

The last walkthrough in this section will demonstrate you how to work with related data.

The first step is to modify the Create Car view to display a drop-down with all available categories from the database. Open the Create view for the Car controller.

Replace the following code:

ASP.NET

@Html.EditorFor(model => model.CategoryID)
@Html.EditorFor(Function(model) model.CategoryID)

With this one:

ASP.NET

@Html.DropDownList( "CategoryId", String.Empty )
@Html.DropDownList( "CategoryId", String.Empty )

Open the CarController and navigate to the first Create method. Add the following code.

    public ActionResult Create()
    {
       this.ViewData.Add( "CategoryID", new SelectList(
           this.dbContext.Categories.ToList(), "CategoryID", "CategoryName" ) );
       return View();
    }
    Function Create() As ActionResult
     Me.ViewData.Add("CategoryID", New SelectList(Me.dbContext.Categories.ToList(), "CategoryID", "CategoryName"))
     Return View()
    End Function

Now, when you run your application and try to add a new Car, you will be able to select an existing category from the drop-down.