Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Working with Related Data
Programmer's Guide > Quick-Start Scenarios > ASP.NET MVC > Working with Related Data

Glossary Item Box

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 - C# Copy Code
@Html.EditorFor(model => model.CategoryID)
ASP.NET - VB.NET Copy Code
@Html.EditorFor(Function(model) model.CategoryID)

With this one:

ASP.NET - C# Copy Code
@Html.DropDownList( "CategoryId", String.Empty )
ASP.NET - VB.NET Copy Code
@Html.DropDownList( "CategoryId", String.Empty )

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

C# Copy Code
public ActionResult Create()
{
   
this.ViewData.Add( "CategoryID", new SelectList(
       
this.dbContext.Categories.ToList(), "CategoryID", "CategoryName" ) );
   
return View();
}
VB.NET Copy Code
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.