Return A Message From ViewModel

Create one controller in our MVC application. Add bellow action method and view for the same.

  1. public ActionResult Index()  
  2.        {  
  3.            return View();  
  4.        } 
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.
  1. <script src="~/Scripts/jquery-2.2.0.js"></script>  
  2. <script src="~/Scripts/knockout-3.4.0.js"></script>  
  3. <script src="~/Scripts/Before-KO.js"></script> 
Now add below code to our Before-KO.js file.
  1. $(document).ready(function () {  
  2.     //debugger;  
  3.     var p = 500;  
  4.     var viewModel = {  
  5.     };  
  6.     viewModel.priceRating = ko.computed(function () {  
  7.         if(p != null)  
  8.         {  
  9.             if (p > 5000) {  
  10.                 return "you can not buy product because it is expensive";  
  11.             }  
  12.             else {  
  13.                 return "you can buy products upto 5000";  
  14.             }  
  15.         }  
  16.     },  
  17.         viewModel);  
  18.     ko.applyBindings(viewModel);  
  19. }) 
Now if we run the application we will get message as
  1. you can buy products upto 5000