Introduction
In my previous article I explained:
- mouseover and mouseout Event Binding Using Knockout.
- enable and disable Binding using Knockout.
- Checked Binding Using Knockout in ASP.NET Application
Today's article explains Options Binding with a Drop-Down List using Knockout.
Step 1
First of all, you need to add an external Knockout js file into your application, you can either download it from the internet or can download my application that is available at the beginning of this article in Zip Format and then can use the file attached with this Zip file.
After downloading the file you need to call it in the head section of your application.
- <head runat="server">
- <title></title>
- <script src="knockout-2.3.0.js"></script>
- </head>
Step 2
Now we can work on our application. First we will work on the ViewModel. For this you need to add a Script tag under the Body Section and then need to add this code:
- <script type="text/javascript">
- function Car(name, price) {
- this.carName = name;
- this.price = price;
- };
- function x() {
- availableCars = ko.observableArray([
- new Car("i10", 450000),
- new Car("120", 700000),
- new Car("Verna", 1200000)
- ]);
- selectedCar = ko.observable();
- };
- ko.applyBindings(new x());
- </script>
Here first I had declared a function named Car, in this function carName and price are declared.
Then another function that is the main function is named "x()" is declared, in this function an Observable and an Observable Array is declared.
In function x() an array is declared that is made Observable. In this array some carName with their Price is declared.
Then an Observable is created named selectedCar.
At the end binding is applied to the function "x()."
Step 3
Now we will work on the view of our application. Write this code in the view part of your application:




Join the conversation! Your thoughts help the community grow.