Create one controller in our MVC application. Add bellow action method and view for the same.
- public ActionResult Index()
- {
- return View();
- }
Now download the latest knockout Js file from here http://knockoutjs.com
Add JavaScripts. I have created Before-KO.js file to add our knockout Js code.
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/knockout-3.4.0.js"></script>
- <script src="~/Scripts/Before-KO.js"></script>
Now add below code to our Before-KO.js file.
- $(document).ready(function () {
-
- var p = 500;
- var viewModel = {
- };
- viewModel.priceRating = ko.computed(function () {
- if(p != null)
- {
- if (p > 5000) {
- return "you can not buy product because it is expensive";
- }
- else {
- return "you can buy products upto 5000";
- }
- }
- },
- viewModel);
- ko.applyBindings(viewModel);
- })
Now if we run the application we will get message as
- you can buy products upto 5000