Aspect Oriented Programming (AOP) is a very powerful approach to avoid boilerplate code and archive better modularity. The main idea is to add behavior (advice) to the existing code without making any changes in the code itself. AOP provides a…
ASP.NET CORE 2.0 Hosting – How To Use StringBuilder And File Class For .NET Application?
In this article, I am going to share how to use StringBuilder and File Class to create an error log for your .NET application. Here, I am going to share how and why should we create error log in our…

ASP.NET CORE 2.0 Hosting – Manage Product And Quickly Add It To Cart
Step 1 Manage product,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
@{ ViewBag.Title = "Index"; // Layout = null; } <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- add thids links for the error message--> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/css/toastr.css" /> <script src="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/js/toastr.js"></script> <!--add this link for the datatable--> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <link href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.csss" rel="stylesheet" /> <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js" integrity="sha256-4F7e4JsAJyLUdpP7Q8Sah866jCOhv72zU5E8lIRER4w=" crossorigin="anonymous"></script> <link href='http://fonts.googleapis.com/css?family=Work+Sans:400,600,700' rel='stylesheet' type='text/css'> <h2>Product Master</h2> <hr /> <style> .red_border { border: 1px solid #e46262; } .LoadingDiv { top: 0; left: 0; position: fixed; opacity: 0.97; z-index: 10000000; background: Black; height: 100%; width: 100%; margin: auto; } .dataTables_filter { float:right; } </style> <div class="col-md-12"> <div class="col-md-4"></div> <div class="col-md-4"> <div class="col-md-12"> <label>Product Name :</label> <input class="form-control required" type="text" id="txtName" required /> </div> <div class="col-md-12"> <label>Product Descreption :</label> <textarea class="form-control required" id="txtDesc"></textarea> </div> <div class="col-md-12"> <label>Product Price :</label> <input class="form-control required" onkeypress = "return isNumberKey(event)" id="txtPrice" type="text" required /> </div> <div class="col-md-12"> <br /> <input id="btnSave" class="btn btn-success" type="button" value="Save Product" /> <input id="btnCancel" class="btn btn-danger" type="button" value="Cancel" /> </div> </div> <div class="col-md-4"></div> </div> <div class="col-md-12"> <table id="tblProduct" class="table table-striped table-bordered" width="100%"> <thead> <tr> <th >Product_ID</th> <th>Name</th> <th>Description</th> <th>Price</th> <th>Action</th> </tr> </thead> </table> </div> <div id="dvLoader" class="LoadingDiv" style="display: none;"> <table style="height: 100%; margin: auto;"> <tr> <td style="vertical-align: middle;"> <center> <img src="http://www.girlsgotit.org/images/ajax-loader.gif" alt="Loading" /> </center> </td> </tr> </table> </div> <input type="hidden" id="hdnPID" value="0" /> <script type="text/javascript"> $(document).ready(function () { windowResize(); $(window).resize(function () { windowResize(); }); $('#toast-container').find('.toast-top-center').removeClass('.toast-top-center'); toastr.options = { "closeButton": true, 'autoWidth': false, "debug": false, "newestOnTop": true, "progressBar": true, "positionClass": "toast-top-center", "preventDuplicates": false, "onclick": null, "showDuration": "300", "hideDuration": "1000", "timeOut": "3000", "extendedTimeOut": "1000", "showEasing": "swing", "hideEasing": "linear", "showMethod": "fadeIn", "hideMethod": "fadeOut" } $("#btnSave").click(function () { var PID = $("#hdnPID").val(); var Name = $("#txtName").val(); var Desc = $("#txtDesc").val(); var Price = parseFloat($("#txtPrice").val()).toFixed(2); if (CheckRequiredFields()) { $('#dvLoader').show(); $.ajax({ url: '@Url.Action("SaveAndUpdateProduct", "Home")', type: 'POST', data: JSON.stringify({ "PID": parseInt(PID), "Name": Name, "Description": Desc, "price": Price }), dataType: "json", contentType: "application/json; charset=utf-8", success: function (result) { $('#dvLoader').hide(); if (result.Status == "True") { toastr.success(result.Message); clear(); display(); } else { toastr.success(result.Message); clear(); display(); } } }); } }); $("#btnCancel").click(function () { clear(); }); }); function CheckRequiredFields() { var isValid = true; $('.required').each(function () { if ($.trim($(this).val()) == '') { isValid = false; $(this).addClass('red_border'); } else { $(this).removeClass('red_border'); } }); return isValid; } function checkemail(valemail) { var forgetfilter = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; if (forgetfilter.test(valemail)) { return true; } else { return false; } } function clear() { $("#txtName").val(""); $("#txtDesc").val(""); $("#txtPrice").val(""); $('#btnSave').val("Save Product"); $("#hdnPID").val(0); } function display() { $('#dvLoader').show(); Table = $('#tblProduct').DataTable({ "processing": true, "serverSide": false, "paging": true, "ordering": true, "info": true, "searching": true, "bFilter": false, "scrollX": "100%", "scrollY": ($(window).height() - 500), "sScrollXInner": "100%", "bScrollCollapse": true, "sAjaxSource": '@Url.Action("GetProduct","Home")', "bDestroy": true, "bLengthChange": true, "bPaginate": true, "sEmptyTable": "Loading data from server", "columns": [ { "sWidth": "5%", "sClass": "TextCenter PID", "render": function (data, type, row) { return row[0]; } }, { "sWidth": "5%", "sClass": "TextCenter Name", "render": function (data, type, row ) { return (row[1]); } }, { "sWidth": "5%", "sClass": "TextCenter Desc", "render": function (data, type, row ) { return (row[2]); } }, { "sWidth": "5%", "sClass": "TextCenter Price", "render": function (data, type, row ) { return (row[3]); } }, { "bSortable": false, "sClass": "TextCenter", "sWidth": "3%", "render": function (data, type, row) { return '<center><a href="javascript:void(0);" onclick=deleteData("' + row[0] + '"); return false;> <i class="glyphicon glyphicon-trash"></i></a> <a href="javascript:void(0);" onclick=EditData(this); return false;> <i class="glyphicon glyphicon-edit"></i></a></center>'; }, "targets": 0, } ], }); $('#dvLoader').hide(); } function windowResize() { display(); }; function deleteData(id) { var PID = parseInt(id); bootbox.confirm({ title: 'Remove Customer', message: 'Are you sure want to delete this record?', buttons: { 'cancel': { label: 'No', className: 'btn-default pull-right' }, 'confirm': { label: 'Yes', className: 'btn-primary margin-right-5' } }, callback: function (result) { if (result) { $('#dvLoader').show(); $.ajax({ url: '@Url.Action("DeleteProduct", "Home")', type: 'POST', data: JSON.stringify({ "id": id }), contentType: 'application/json; charset=utf-8;', success: function (result) { windowResize(); $('#dvLoader').hide(); if (result.Status == "True") { toastr.success(result.Message); clear(); } else { toastr.success(result.Message); } } }); } } }); } function EditData(row) { debugger var PID = $(row).closest('tr').find('.PID').html(); $("#hdnPID").val(parseInt(PID)); var Name = $(row).closest('tr').find('.Name').html(); $('#txtName').val(Name); var Desc = $(row).closest('tr').find('.Desc').html(); $('#txtDesc').val(Desc); var Price = $(row).closest('tr').find('.Price').html(); $('#txtPrice').val(Price); $('#btnSave').val("Update Product"); } function isNumberKey(evt) { var charcode = (evt.which) ? evt.which : evt.keycode if (charcode > 31 && (charcode < 48 || charcode > 58) && evt.keyCode != 35 && evt.keyCode != 36 && evt.keyCode != 37 && evt.keyCode != 38 && evt.keyCode != 39 && evt.keyCode != 40 && evt.keyCode != 46) { return false; } else { return true; } } </script> |
Step 2 Code of controller to manage product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Mvc; namespace DataTable_Demo.Controllers { public class HomeController : Controller { // // GET: /Home/ private PMSEntities context = new PMSEntities(); public ActionResult Index() { return View(); } public JsonResult SaveAndUpdateProduct(int PID,string Name, string Description, float Price) { var result = new jsonMessage(); try { //define the model Mst_Product _Mst_Product = new Mst_Product(); _Mst_Product.PID = PID; _Mst_Product.Name = Name; _Mst_Product.Description = Description; _Mst_Product.Price = Price; //for insert recored.. if (_Mst_Product.PID == 0) { context.Mst_Product.Add(_Mst_Product); result.Message = "your product has been saved success.."; result.Status = true; } else //for update recored.. { context.Entry(_Mst_Product).State = EntityState.Modified; result.Message = "your product has been updated successfully.."; result.Status = true; } context.SaveChanges(); } catch (Exception ex) { ErrorLogers.ErrorLog(ex); result.Message = "We are unable to process your request at this time. Please try again later."; result.Status = false; } return Json(result, JsonRequestBehavior.AllowGet); } public JsonResult GetProduct() { List<Mst_Product> _list = new List<Mst_Product>(); try { _list = context.Mst_Product.ToList(); var result = from c in _list select new[] { Convert.ToString( c.PID ), // 0 Convert.ToString( c.Name ), // 1 Convert.ToString( c.Description ), // 2 Convert.ToString( c.Price ), // 3 }; return Json(new { aaData= result }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { ErrorLogers.ErrorLog(ex); return Json(new { aaData = new List<string[]> { } }, JsonRequestBehavior.AllowGet); } } public JsonResult DeleteProduct(int id) { var result = new jsonMessage(); try { var product = new Mst_Product { PID = id }; context.Entry(product).State = EntityState.Deleted; context.SaveChanges(); result.Message = "your product has been deleted successfully.."; result.Status = true; } catch (Exception ex) { ErrorLogers.ErrorLog(ex); result.Message = "We are unable to process your request at this time. Please try again later."; result.Status = false; } return Json(result, JsonRequestBehavior.AllowGet); } } } |
Step 3 Add the Product controller and product view for the show product page List.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace DataTable_Demo.Controllers { public class ProductController : Controller { // // GET: /Product/ private PMSEntities context = new PMSEntities(); public ActionResult Product() { List<Mst_Product> _list = new List<Mst_Product>(); _list = context.Mst_Product.ToList(); return View(_list); } public JsonResult AddToCart(int PID) { var result = new jsonMessage(); try { Mst_Product _Mst_Product = context.Mst_Product.Where(t => t.PID == PID).FirstOrDefault(); //define the model of crt Cart _Cart = new Cart(); _Cart.PID = PID; _Cart.Quantity = 1; _Cart.DateTime = System.DateTime.Now; _Cart.TotalPrice = Convert.ToDouble(_Mst_Product.Price); context.Carts.Add(_Cart); result.Message = "your product has been Added in to cart.."; result.Status = true; context.SaveChanges(); } catch (Exception ex) { ErrorLogers.ErrorLog(ex); result.Message = "We are unable to process your request at this time. Please try again later."; result.Status = false; } return Json(result, JsonRequestBehavior.AllowGet); } } } @{ ViewBag.Title = "Product"; Layout = "~/Views/Shared/_Layout.cshtml"; } <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- add thids links for the error message--> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/css/toastr.css" /> <script src="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/js/toastr.js"></script> @*<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <div class="w3-panel w3-animate-opacity"> <div class="w3-tag w3-jumbo">S</div> <div class="w3-tag w3-jumbo">A</div> <div class="w3-tag w3-jumbo">L</div> <div class="w3-tag w3-jumbo">E</div> </div> <div class="w3-row-padding"> @foreach (var item in Model) { <div class="w3-third"> <div class="w3-card"> <div class="w3-container w3-red"> <h1><b>@item.Name</b></h1> </div> <div class="w3-container w3-xlarge"> <p> @item.Name<br> <i><b>@item.Description</b></i><br> <span class="w3-xxlarge w3-text-red"><b>@item.Name</b></span> </p> <p> <del>$400</del> <span class="w3-tag w3-yellow">New!</span><br> Now only @item.Price !!! </p> </div> <div class="w3-container w3-red"> <p class="w3-opacity">Contact: Bhavdip-9825891108</p> </div> </div> <br /> </div> } </div>*@ <!doctype html> <html lang="en" class="no-js"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href='http://fonts.googleapis.com/css?family=Work+Sans:400,600,700' rel='stylesheet' type='text/css'> <link href="~/Content/css/reset.css" rel="stylesheet" /> <link href="~/Content/css/style.css" rel="stylesheet" /> <script src="~/Content/js/modernizr.js"></script> <title>Document</title> </head> <style> .red_border { border: 1px solid #e46262; } .LoadingDiv { top: 0; left: 0; position: fixed; opacity: 0.97; z-index: 10000000; background: Black; height: 100%; width: 100%; margin: auto; } .dataTables_filter { float:right; } </style> <body> <div id="dvLoader" class="LoadingDiv" style="display: none;"> <table style="height: 100%; margin: auto;"> <tr> <td style="vertical-align: middle;"> <center> <img src="http://www.girlsgotit.org/images/ajax-loader.gif" alt="Loading" /> </center> </td> </tr> </table> </div> <header> <h1>Quick Add to Cart</h1> </header> <a href="@Url.Action("Cart","Cart")" class="cd-cart"> <span>0</span> </a> <ul class="cd-gallery"> @foreach (var item in Model) { <li> <div class="cd-single-item"> <a href="#0"> <ul class="cd-slider-wrapper"> <li> <img src="~/Content/img/thumb-1.jpg" alt="Preview image"></li> <li> <img src="~/Content/img/thumb-2.jpg" alt="Preview image"></li> <li class="selected"> <img src="~/Content/img/thumb-1.jpg" alt="Preview image"></li> </ul> </a> <div class="cd-customization"> <div class="color selected-3" data-type="select"> <ul> <li class="color-1">color-1</li> <li class="color-2">color-2</li> <li class="color-3 active">color-3</li> </ul> </div> <div class="size" data-type="select"> <ul> <li class="small active">Small</li> <li class="medium">Medium</li> <li class="large">Large</li> </ul> </div> <button class="add-to-cart" onclick="AddToCart('@item.PID');"> <em>Add to Cart</em> <svg x="0px" y="0px" width="32px" height="32px" viewBox="0 0 32 32"> <path stroke-dasharray="19.79 19.79" stroke-dashoffset="19.79" fill="none" stroke="#FFFFFF" stroke-width="2" stroke-linecap="square" stroke-miterlimit="10" d="M9,17l3.9,3.9c0.1,0.1,0.2,0.1,0.3,0L23,11" /> </svg> </button> </div> <!-- .cd-customization --> <button class="cd-customization-trigger">Customize</button> </div> <!-- .cd-single-item --> <div class="cd-item-info"> <b><a href="#0">@item.Name</a></b> <br /><span style="font-weight:normal;font-family:Arial;font-size:15px;color:gray">@item.Description</span> <em>$@item.Price</em> </div> <!-- cd-item-info --> </li> } </ul> <!-- cd-gallery --> @*<script src="js/jquery-2.1.4.js"></script>*@ <script src="~/Content/js/main.js"></script> <!-- Resource jQuery --> </body> </html> <script type="text/javascript"> $(document).ready(function () { }); function AddToCart(PID) { var PID = PID $('#dvLoader').show(); $.ajax({ url: '@Url.Action("AddToCart", "Product")', type: 'POST', data: JSON.stringify({ "PID": parseInt(PID) }), dataType: "json", contentType: "application/json; charset=utf-8", success: function (result) { $('#dvLoader').hide(); if (result.Status == "True") { toastr.success(result.Message); clear(); display(); } else { toastr.success(result.Message); clear(); display(); } } }); } </script> Step 4 Add the cart controller and add the cart view using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Mvc; namespace DataTable_Demo.Controllers { public class CartController : Controller { // // GET: /Cart/ private PMSEntities context = new PMSEntities(); public class Cart_Deatails { public int PID { get; set; } public string Name { get; set; } public string Description { get; set; } public Nullable<double> Price { get; set; } public int Quantity { get; set; } public DateTime Date { get; set; } public double TotalPrice { get; set; } } public ActionResult Cart() { var joinedResult = (from u in context.Mst_Product join u2 in context.Carts on u.PID equals u2.PID select new Cart_Deatails { PID=u.PID, Name = u.Name, Description = u.Description, Price = u.Price, Quantity=u2.Quantity, Date=u2.DateTime, TotalPrice=u2.TotalPrice }).ToList(); return View(joinedResult); } public ActionResult Invoice() { var joinedResult = (from u in context.Mst_Product join u2 in context.Carts on u.PID equals u2.PID select new Cart_Deatails { PID = u.PID, Name = u.Name, Description = u.Description, Price = u.Price, Quantity = u2.Quantity, Date = u2.DateTime, TotalPrice = u2.TotalPrice }).ToList(); return View(joinedResult); } public JsonResult DeleteProduct(int id) { var result = new jsonMessage(); try { var a = context.Carts.Where(x => x.PID == id).FirstOrDefault(); context.Carts.Remove(a); context.SaveChanges(); result.Message = "your product has been removed successfully.."; result.Status = true; } catch (Exception ex) { ErrorLogers.ErrorLog(ex); result.Message = "We are unable to process your request at this time. Please try again later."; result.Status = false; } return Json(result, JsonRequestBehavior.AllowGet); } public JsonResult UpdateCart(int id, string Qty, string FinalAmount) { var result = new jsonMessage(); try { Cart a = context.Carts.Where(x => x.PID == id).FirstOrDefault(); //define the model a.PID = Convert.ToInt32(id); a.Quantity = Convert.ToInt32(Qty); a.TotalPrice = Convert.ToDouble(FinalAmount); //for Update cart.. context.Entry(a).State = EntityState.Modified; result.Message = ""; result.Status = true; context.SaveChanges(); } catch (Exception ex) { ErrorLogers.ErrorLog(ex); result.Message = "We are unable to process your request at this time. Please try again later."; result.Status = false; } return Json(result, JsonRequestBehavior.AllowGet); } } } @{ ViewBag.Title = "Cart"; Layout = "~/Views/Shared/_Layout.cshtml"; var count = 0; } <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- add thids links for the error message--> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/css/toastr.css" /> <script src="http://cdnjs.cloudflare.com/ajax/libs/toastr.js/1.3.1/js/toastr.js"></script> <div class="container"> <div class="row"> <div class="col-sm-12 col-md-10 col-md-offset-1"> <table class="table table-hover"> <thead> <tr> <th>Product</th> <th>Quantity</th> <th class="text-center">Price</th> <th class="text-center">Total</th> <th> </th> </tr> </thead> <tbody> @for(int i=0;i<Model.Count;i++) { count = Model.Count; <tr> <td class="col-sm-8 col-md-6"> <div class="media"> <a class="thumbnail pull-left" href="#"> <img class="media-object" src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-2/72/product-icon.png" style="width: 72px; height: 72px;"> </a> <div class="media-body"> <h4 class="media-heading"><a href="#">@Model[i].Name</a></h4> <h5 class="media-heading"><a href="#">@Model[i].Description</a></h5> <span>Status: </span><span class="text-success"><strong>In Stock</strong></span> </div> </div></td> <td class="col-sm-1 col-md-1" style="text-align: center"> <input type="email" class="form-control" id="[email protected]" onkeypress = "return isNumberKey(event)" onkeyup="counttotal('@Model[i].Price',@i,@Model[i].PID);" value="@Model[i].Quantity"> </td> <td class="col-sm-1 col-md-1 text-center"><strong id="[email protected]">@Model[i].Price</strong></td> <td class="col-sm-1 col-md-1 text-center"><strong id="[email protected]">@Model[i].TotalPrice</strong></td> <td class="col-sm-1 col-md-1"> <button type="button" class="btn btn-danger" onclick="removeproduct('@Model[i].PID')"> <span class="glyphicon glyphicon-remove"></span> Remove </button></td> </tr> } <tr> <td> </td> <td> </td> <td> </td> <td><h5>Subtotal</h5></td> <td class="text-right"><h5><strong id="setprice">0</strong></h5></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td><h5>Estimated shipping</h5></td> <td class="text-right"><h5><strong id="">0</strong></h5></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td><h3>Total</h3></td> <td class="text-right"><h3><strong id="setfinaltotal">0</strong></h3></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> <button type="button" class="btn btn-default"> <span class="glyphicon glyphicon-shopping-cart"></span> Continue Shopping </button></td> <td> <button type="button" class="btn btn-success" onclick="generateinvoice();"> Checkout <span class="glyphicon glyphicon-play"></span> </button></td> </tr> </tbody> </table> </div> </div> </div> <script type="text/ecmascript"> $(document).ready(function () { Calculateinvoice(); }); function generateinvoice() { window.location.href = '@Url.Action("Invoice", "Cart")'; //TempData["finaltotal"] = $("#setfinaltotal").val(); //TempData["subtotal"] = $("#setprice").val(); } function removeproduct(pid) { var PID = pid; $('#dvLoader').show(); $.ajax({ url: '@Url.Action("DeleteProduct", "Cart")', type: 'POST', data: JSON.stringify({ "id": PID }), contentType: 'application/json; charset=utf-8;', success: function (result) { $('#dvLoader').hide(); if (result.Status == "True") { toastr.success(result.Message); location.reload(); } else { toastr.success(result.Message); location.reload(); } } }); } function Calculateinvoice() { var Price = 0.0; var FinalToatal = 0.0; for (var i = 0; i < 2; i++) { Price += Number(($("#pricecount_" + i).text())); FinalToatal += Number(($("#finaltotal_" + i).text())); } $("#setprice").text(parseFloat(Price).toFixed(2)); $("#setfinaltotal").text(parseFloat(FinalToatal).toFixed(2)); } function counttotal(price, i,pid) { var PID = pid; var price=parseFloat(price).toFixed(2); var qnt = parseInt($("#exampleInputEmail1_" + i).val()); if (qnt !== qnt) { $("#exampleInputEmail1_" + i).val(1); qnt = 1; } var total = price * qnt; $("#finaltotal_"+i).text(total); Calculateinvoice(); updatecart(PID, qnt, total); } function updatecart(PID, qnt, total) { PID = parseInt(PID); $.ajax({ url: '@Url.Action("UpdateCart", "Cart")', type: 'POST', data: JSON.stringify({ "id": PID, "Qty": qnt, "FinalAmount": total }), contentType: 'application/json; charset=utf-8;', success: function (result) { } }); } function isNumberKey(evt) { var charcode = (evt.which) ? evt.which : evt.keycode if (charcode > 31 && (charcode < 48 || charcode > 58) && evt.keyCode != 35 && evt.keyCode != 36 && evt.keyCode != 37 && evt.keyCode != 38 && evt.keyCode != 39 && evt.keyCode != 40 && evt.keyCode != 46) { return false; } else { return true; } } </script> |

ASP.NET CORE 2.0 Hosting – How To Use Session Value Client Side Using JavaScript In ASP.NET?
In this article, I am going to explain how to use the value of the session variable at client-side using JavaScript in ASP.NET with an example. By using Session property, you can easily access the values of the session in…
ASP.NET CORE 2.0 Hosting – Typesafe .Net Object Comparison using ZCompare
ZCompare is a powerful autonomous typesafe .NET object comparison tool. The modified data scenario where changes have been made to the original data is a common problem and essentially, as developers, we want to know what has beed added, deleted…

ASP.NET CORE 2.0 Hosting – Validation Tag Helpers In ASP.NET Core MVC
This article explains about the validation tag helpers of ASP.NET Core MVC. There are two validation tag helpers available. Validation Message Tag Helper: It displays the validation message for a single property of a Model. Validation Summary Tag Helper: It…

ASP.NET CORE 2.0 Hosting – Developers Use Case For .NET API Browser
Many times, developers need to know, if a technology or a feature is supported by a specific .NET version. For example, how to verify that .NET Core has Linux support or not. Well, you can always Bing that but wouldn’t…
ASP.NET CORE 2.0 Hosting – Client Side Validations In .NET Web Applications With JavaScript /jQuery
Now, we will learn how to perform validations at the client side with the help of JavaScript/jQuery. The main advantage of this approach is given below. We are able to improve the performance of our web application, since it runs…
ASP.NET CORE 2.0 Hosting – Post The Data To ASP.NET MVC Controller Using JQuery Ajax
This blog will demonstrate, how to post the data to ASP.NET MVC controller(s) using JQuery Ajax. For that, I have created one controller “JQueryAjaxCallController” with the post action method “AjaxPostCall” and a class “Employee” as below. Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace JQueryAjaxCallINMVC.Controllers { public class JQueryAjaxCallController: Controller { // // GET: /JQueryAjaxCall/ public ActionResult Index() { return View(); } [HttpPost] public JsonResult AjaxPostCall(Employee employeeData) { Employee employee = new Employee { Name = employeeData.Name, Designation = employeeData.Designation, Location = employeeData.Location }; return Json(employee, JsonRequestBehavior.AllowGet); } } public class Employee { public string Name { get; set; } public string Designation { get; set; } public string Location { get; set; } } } |
View…
ASP.NET CORE 2.0 Hosting – Manage Product And Quickly Add It To Cart
In this post I will tell you about how to manage product and Quickly add it to cart. Manage product in ASP.NET. @{ ViewBag.Title = “Index”; // Layout = null; } <!– Latest compiled and minified CSS –> <!– jQuery…