Quick Start: ListBox ASP.NET MVC

This topic demonstrates how to add a ListBox control to your application. For information on how to add ASP.NET MVC Edition controls, see Adding Controls. This topic comprises of three steps:

The following image shows how the ListBox appears after completing the steps above. The example uses model Cities.cs added in the ComboBox QuickStart.

Step 1: Add a new Controller and View

Add a new Controller:

  1. In the Solution Explorer, right click the folder Controllers.
  2. From the context menu, select Add | Controller. The Add Controller dialog appears.
  3. Complete the following steps in the Add Controller dialog:
    1. Set name of the controller (for example: Default1Controller).
    2. Select Empty MVC Controller template.
    3. Click Add.
  4. A new controller is added to the application.
  5. Replace the method Index() with the following method.

    C#

    Copy Code

~~~
public ActionResult Index()  
{  
    ViewBag.Cities = MVCFlexGrid.Models.Cities.GetCities();  
    return View();  
}
~~~

Add a View for the controller:

  1. From the Solution Explorer, expand the folder Controllers and double click the controller (for example: Default1Controller) to open it.
  2. Place the cursor inside the method Index().
  3. Right click and select Add View. The Add View dialog appears.
  4. In the Add View dialog, verify that the View name is Index and View engine is Razor (CSHTML).
  5. Click Add. A view is added for the controller.

Step 2: Add the Control

  1. From the Solution Explorer, expand the folder Views.
  2. Double click Index.cshtml to open it.
  3. Replace the default code of the Index.cshtml file with the code given below to initialize a ListBox control.

    Razor

    Copy Code

~~~
@{  
    List<string> cities = ViewBag.Cities;  
}  
<div>  
    <label>Select your city:</label>  
    @(Html.C1().ListBox()  
        .Bind(cities)  
        .Width(250).Height(150)  
    )  
</div>
~~~

Step 3: Build and Run the Project

  1. Click Build | Build Solution to build the project.
  2. Press F5 to run the project.

    Append the folder name and view name to the generated URL (for example: http://localhost:1234/**Default1/index**) in the address bar of the browser to see the view. Or link the view to the home page.

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus