Dec 19, 2014

Knockout CRUD Operations using MVC4

As you know Knockout is a javascript library that allow you to bind html elements to any data model. You can also use Knockout for Insert, Update, Delete and Retrieve operations. This article will demonstrate, how to use Knockout with MVC4 for CRUD (Create, Read, Update, Delete) Operations.


Step 1: Define Model

Product.cs
  1. public class Product
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public string Category { get; set; }
  6. public decimal Price { get; set; }
  7. }
IProductRepository.cs
  1. interface IProductRepository
  2. {
  3. IEnumerable<Product> GetAll();
  4. Product Get(int id);
  5. Product Add(Product item);
  6. bool Update(Product item);
  7. bool Delete(int id);
  8. }
ProductRepository.cs
  1. public class ProductRepository : IProductRepository
  2. {
  3. private List<Product> products = new List<Product>();
  4. private int _nextId = 1;
  5.  
  6. public ProductRepository()
  7. {
  8. // Add products for the Demonstration
  9. Add(new Product { Name = "Computer", Category = "Electronics", Price = 23.54M });
  10. Add(new Product { Name = "Laptop", Category = "Electronics", Price = 33.75M });
  11. Add(new Product { Name = "iPhone4", Category = "Phone", Price = 16.99M });
  12. }
  13.  
  14. public IEnumerable<Product> GetAll()
  15. {
  16. // TO DO : Code to get the list of all the records in database
  17. return products;
  18.  
  19. }
  20.  
  21. public Product Get(int id)
  22. {
  23. // TO DO : Code to find a record in database
  24. return products.Find(p => p.Id == id);
  25.  
  26. }
  27.  
  28. public Product Add(Product item)
  29. {
  30. if (item == null)
  31. {
  32. throw new ArgumentNullException("item");
  33. }
  34. // TO DO : Code to save record into database
  35. item.Id = _nextId++;
  36. products.Add(item);
  37.  
  38. return item;
  39. }
  40.  
  41. public bool Update(Product item)
  42. {
  43. if (item == null)
  44. {
  45. throw new ArgumentNullException("item");
  46. }
  47. // TO DO : Code to update record into database
  48. int index = products.FindIndex(p => p.Id == item.Id);
  49. if (index == -1)
  50. {
  51. return false;
  52. }
  53. products.RemoveAt(index);
  54. products.Insert(index,item);
  55.  
  56. return true;
  57. }
  58.  
  59. public bool Delete(int id)
  60. {
  61. // TO DO : Code to remove the records from database
  62. products.RemoveAll(p => p.Id == id);
  63.  
  64. return true;
  65. }
  66. }

Step 2: Define Controller

ProductController.cs
  1. public class ProductController : Controller
  2. {
  3. static readonly IProductRepository repository = new ProductRepository();
  4.  
  5. public ActionResult Product()
  6. {
  7. return View();
  8. }
  9.  
  10. public JsonResult GetAllProducts()
  11. {
  12. return Json(repository.GetAll(), JsonRequestBehavior.AllowGet);
  13. }
  14.  
  15. public JsonResult AddProduct(Product item)
  16. {
  17. item = repository.Add(item);
  18. return Json(item, JsonRequestBehavior.AllowGet);
  19. }
  20.  
  21. public JsonResult EditProduct(int id, Product product)
  22. {
  23. product.Id = id;
  24. if (repository.Update(product))
  25. {
  26. return Json(repository.GetAll(), JsonRequestBehavior.AllowGet);
  27. }
  28.  
  29. return Json(null);
  30. }
  31.  
  32. public JsonResult DeleteProduct(int id)
  33. {
  34.  
  35. if (repository.Delete(id))
  36. {
  37. return Json(new { Status = true }, JsonRequestBehavior.AllowGet);
  38. }
  39.  
  40. return Json(new { Status = false }, JsonRequestBehavior.AllowGet);
  41.  
  42.  
  43. }
  44. }

Step 3: Define View

Product.cshtml
  1. @{
  2. ViewBag.Title = "Products' List";
  3. }
  4. @section scripts {
  5. <style type="text/css">
  6. body {
  7. margin: 20px;
  8. font-family: "Arial", "Helventica", sans-serif;
  9. }
  10.  
  11. label {
  12. width: 80px;
  13. display: inline-block;
  14. }
  15.  
  16. button {
  17. display: inline-block;
  18. outline: none;
  19. cursor: pointer;
  20. text-align: center;
  21. text-decoration: none;
  22. padding: .4em 1.1em .4em;
  23. color: #fef4e9;
  24. border: solid 1px #006fb9;
  25. background: #1276bb;
  26. }
  27.  
  28. button:hover {
  29. text-decoration: none;
  30. background: #282828;
  31. border: solid 1px #000;
  32. }
  33.  
  34. table {
  35. padding-top: 1em;
  36. }
  37.  
  38. thead, tfoot {
  39. font-weight: 600;
  40. }
  41.  
  42. th, td {
  43. padding: .1em .5em;
  44. text-align: left;
  45. }
  46.  
  47. td li, td ul {
  48. margin: 0;
  49. padding: 0;
  50. }
  51.  
  52. td li {
  53. display: inline;
  54. }
  55.  
  56. td li::after {
  57. content: ',';
  58. }
  59.  
  60. td li:last-child::after {
  61. content: '';
  62. }
  63. </style>
  64.  
  65. <script src="~/Scripts/jquery-1.8.2.min.js"></script>
  66. <script src="~/Scripts/knockout-2.2.0.js"></script>
  67. <script src="~/Scripts/knockout-2.2.0.debug.js"></script>
  68.  
  69. <script type="text/javascript">
  70. function formatCurrency(value) {
  71. return "$" + value.toFixed(2);
  72. }
  73.  
  74. function ProductViewModel() {
  75.  
  76. //Make the self as 'this' reference
  77. var self = this;
  78. //Declare observable which will be bind with UI
  79. self.Id = ko.observable("");
  80. self.Name = ko.observable("");
  81. self.Price = ko.observable("");
  82. self.Category = ko.observable("");
  83.  
  84. var Product = {
  85. Id: self.Id,
  86. Name: self.Name,
  87. Price: self.Price,
  88. Category: self.Category
  89. };
  90.  
  91. self.Product = ko.observable();
  92. self.Products = ko.observableArray(); // Contains the list of products
  93.  
  94. // Initialize the view-model
  95. $.ajax({
  96. url: '@Url.Action("GetAllProducts", "Product")',
  97. cache: false,
  98. type: 'GET',
  99. contentType: 'application/json; charset=utf-8',
  100. data: {},
  101. success: function (data) {
  102. self.Products(data); //Put the response in ObservableArray
  103. }
  104. });
  105.  
  106. // Calculate Total of Price After Initialization
  107. self.Total = ko.computed(function () {
  108. var sum = 0;
  109. var arr = self.Products();
  110. for (var i = 0; i < arr.length; i++) {
  111. sum += arr[i].Price;
  112. }
  113. return sum;
  114. });
  115.  
  116. //Add New Item
  117. self.create = function () {
  118. if (Product.Name() != "" && Product.Price() != "" && Product.Category() != "") {
  119. $.ajax({
  120. url: '@Url.Action("AddProduct", "Product")',
  121. cache: false,
  122. type: 'POST',
  123. contentType: 'application/json; charset=utf-8',
  124. data: ko.toJSON(Product),
  125. success: function (data) {
  126. self.Products.push(data);
  127. self.Name("");
  128. self.Price("");
  129. self.Category("");
  130.  
  131. }
  132. }).fail(
  133. function (xhr, textStatus, err) {
  134. alert(err);
  135. });
  136.  
  137. }
  138. else {
  139. alert('Please Enter All the Values !!');
  140. }
  141.  
  142. }
  143. // Delete product details
  144. self.delete = function (Product) {
  145. if (confirm('Are you sure to Delete "' + Product.Name + '" product ??')) {
  146. var id = Product.Id;
  147.  
  148. $.ajax({
  149. url: '@Url.Action("AddProduct", "Product")',
  150. cache: false,
  151. type: 'POST',
  152. contentType: 'application/json; charset=utf-8',
  153. data: ko.toJSON(id),
  154. success: function (data) {
  155. self.Products.remove(Product);
  156.  
  157. }
  158. }).fail(
  159. function (xhr, textStatus, err) {
  160. self.status(err);
  161. });
  162. }
  163. }
  164.  
  165. // Edit product details
  166. self.edit = function (Product) {
  167. self.Product(Product);
  168.  
  169. }
  170.  
  171. // Update product details
  172. self.update = function () {
  173. var Product = self.Product();
  174.  
  175. $.ajax({
  176. url: '@Url.Action("EditProduct", "Product")',
  177. cache: false,
  178. type: 'PUT',
  179. contentType: 'application/json; charset=utf-8',
  180. data: ko.toJSON(Product),
  181. success: function (data) {
  182. self.Products.removeAll();
  183. self.Products(data); //Put the response in ObservableArray
  184. self.Product(null);
  185. alert("Record Updated Successfully");
  186. }
  187. })
  188. .fail(
  189. function (xhr, textStatus, err) {
  190. alert(err);
  191. });
  192. }
  193.  
  194. // Reset product details
  195. self.reset = function () {
  196. self.Name("");
  197. self.Price("");
  198. self.Category("");
  199. }
  200.  
  201. // Cancel product details
  202. self.cancel = function () {
  203. self.Product(null);
  204.  
  205. }
  206. }
  207. var viewModel = new ProductViewModel();
  208. ko.applyBindings(viewModel);
  209.  
  210. </script>
  211. }
  212.  
  213. <div id="body">
  214.  
  215. <h2>Knockout CRUD Operations with MVC4</h2>
  216.  
  217. <h3>List of Products</h3>
  218.  
  219. <table id="products1" data-bind="visible: Products().length > 0">
  220. <thead>
  221. <tr>
  222. <th>ID</th>
  223. <th>Name</th>
  224. <th>Category</th>
  225. <th>Price</th>
  226. <th>Actions</th>
  227. </tr>
  228. </thead>
  229. <tbody data-bind="foreach: Products">
  230. <tr>
  231. <td data-bind="text: Id"></td>
  232. <td data-bind="text: Name"></td>
  233. <td data-bind="text: Category"></td>
  234. <td data-bind="text: formatCurrency(Price)"></td>
  235.  
  236. <td>
  237. <button data-bind="click: $root.edit">Edit</button>
  238. <button data-bind="click: $root.delete">Delete</button>
  239.  
  240. </td>
  241.  
  242. </tr>
  243. </tbody>
  244. <tfoot>
  245. <tr>
  246. <td></td>
  247. <td></td>
  248. <td>Total :</td>
  249. <td data-bind="text: formatCurrency($root.Total())"></td>
  250. <td></td>
  251. </tr>
  252. </tfoot>
  253. </table>
  254. <br />
  255. <div style="border-top: solid 2px #282828; width: 430px; height: 10px"> </div>
  256.  
  257. <div data-bind="if: Product">
  258. <div>
  259. <h2>Update Product</h2>
  260. </div>
  261. <div>
  262. <label for="productId" data-bind="visible: false">ID</label>
  263. <label data-bind="text: Product().Id, visible: false"></label>
  264.  
  265. </div>
  266. <div>
  267. <label for="name">Name</label>
  268. <input data-bind="value: Product().Name" type="text" title="Name" />
  269. </div>
  270.  
  271. <div>
  272. <label for="category">Category</label>
  273. <input data-bind="value: Product().Category" type="text" title="Category" />
  274. </div>
  275.  
  276. <div>
  277. <label for="price">Price</label>
  278. <input data-bind="value: Product().Price" type="text" title="Price" />
  279.  
  280. </div>
  281. <br />
  282. <div>
  283. <button data-bind="click: $root.update">Update</button>
  284. <button data-bind="click: $root.cancel">Cancel</button>
  285.  
  286. </div>
  287. </div>
  288.  
  289. <div data-bind="ifnot: Product()">
  290. <div>
  291. <h2>Add New Product</h2>
  292. </div>
  293. <div>
  294. <label for="name">Name</label>
  295. <input data-bind="value: $root.Name" type="text" title="Name" />
  296. </div>
  297.  
  298. <div>
  299. <label for="category">Category</label>
  300. <input data-bind="value: $root.Category" type="text" title="Category" />
  301. </div>
  302.  
  303. <div>
  304. <label for="price">Price</label>
  305. <input data-bind="value: $root.Price" type="text" title="Price" />
  306. </div>
  307. <br />
  308. <div>
  309. <button data-bind="click: $root.create">Save</button>
  310. <button data-bind="click: $root.reset">Reset</button>
  311.  
  312. </div>
  313. </div>
  314. </div>

Step 4: Run Application

Retrieve Operation

Create Operation

Update Operation

Delete Operation

I hope you will enjoy the tips while working with Knockout. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
  • 0Blogger Comment
  • Facebook Comment

Leave your comment

Post a Comment