Tri Setia

Tri Setia

  • 1.3k
  • 464
  • 24.2k

Sweet Alert message in ASP.NET CORE MVC

Apr 10 2021 12:08 PM
hi guys,, I'm learn how to use sweet alert in ASP.NET CORE MVC, data successfully saved in database but why the message information successfully saved not appear. Where it the mistake in this code. Any help could be appriciate.
 
this is the DokterController.cs
  1. using Microsoft.AspNetCore.Mvc;    
  2. using System;    
  3. using System.Collections.Generic;    
  4. using System.Linq;    
  5. using System.Threading.Tasks;    
  6. using AdminLTEMvc.Models;    
  7. using Microsoft.Extensions.Configuration;    
  8. using System.Data;    
  9. using Microsoft.Data.SqlClient;    
  10. using Microsoft.Data;    
  11.     
  12. namespace AdminLTEMvc.Controllers    
  13. {    
  14.     public class DokterController : Controller    
  15.     {    
  16.         private readonly IConfiguration _configuration;    
  17.     
  18.         public DokterController(IConfiguration configuration)    
  19.         {    
  20.             this._configuration = configuration;    
  21.         }    
  22.         public IActionResult Index()    
  23.         {    
  24.             DataTable dt = new DataTable();    
  25.             using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_klinik")))    
  26.             {    
  27.                 con.Open();    
  28.                 SqlCommand cmd = new SqlCommand("Select * From Dokter", con);    
  29.                 cmd.CommandType = CommandType.Text;    
  30.                 SqlDataAdapter adap = new SqlDataAdapter(cmd);    
  31.                 adap.Fill(dt);    
  32.             }    
  33.             return View(dt);    
  34.         }    
  35.         [HttpGet]    
  36.         public IActionResult TambahData()    
  37.         {    
  38.             ViewBag.pesan = TempData["pesan"] as string;    
  39.             return View();    
  40.         }    
  41.         public IActionResult TambahData(DokterModel dokterModel)    
  42.         {    
  43.             if (ModelState.IsValid)    
  44.             {    
  45.                 using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_klinik")))    
  46.                 {    
  47.                     using (SqlCommand cmd = new SqlCommand("Insert Into Dokter (Nama_Dokter,Alamat,Jenis_Kelamin,Spesialisasi,Keahlian) Values ('" + dokterModel.Nama_Dokter + "','" + dokterModel.Alamat + "','" + dokterModel.Jenis_Kelamin + "','" + dokterModel.Spesialisasi + "','" + dokterModel.Keahlian + "')", con))    
  48.                     {    
  49.                         con.Open();    
  50.                         cmd.Connection = con;    
  51.                         cmd.CommandType = CommandType.Text;    
  52.                         cmd.ExecuteNonQuery();    
  53.                         TempData["pesan"] = "Data Berhasil disimpan...";    
  54.                         return RedirectToAction("Index""Dokter");    
  55.     
  56.                     }    
  57.                 }    
  58.             }    
  59.             else    
  60.             {    
  61.                 return View(dokterModel);    
  62.             }    
  63.         }  
  64.     }  
  65. }  
This is the TambahData.cshtml
  1. @addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers    
  2. @model AdminLTEMvc.Models.DokterModel    
  3.     
  4. @{    
  5.     Layout = "~/Views/Shared/AdminLayout.cshtml";    
  6. }    
  7. <!DOCTYPE html>    
  8.     
  9. <html>    
  10. <head>    
  11.     <meta name="viewport" content="width=device-width" />    
  12.     <title>Tambah Data Dokter</title>    
  13.     <script src="~/plugins/jquery/jquery.min.js"></script>    
  14.     <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>    
  15.     <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>    
  16.     <link href="~/dist/css/adminlte.min.css" rel="stylesheet" />    
  17.     <script src="~/plugins/bootstrap/js/bootstrap.min.js"></script>    
  18.     <script src="~/plugins/sweetalert2/sweetalert2.js"></script>    
  19.     <link href="~/plugins/sweetalert2/sweetalert2.css" rel="stylesheet" />    
  20.     
  21.     <script type="text/javascript">    
  22.         var Pesan = "@((string)ViewBag.pesan)";    
  23.            if (strvalue !== null && strvalue !== '')    
  24.            {    
  25.                swal("Good job!",Pesan, "success")    
  26.         };    
  27.     </script>    
  28.     
  29. </head>    
  30. <body>    
  31.     <!-- Main content -->    
  32.     <section class="content">    
  33.         <div class="container-fluid">    
  34.             <!-- Horizontal Form -->    
  35.             <div class="card card-green">    
  36.                 <div class="card-header">    
  37.                     <h3 class="card-title">Tambah Data Dokter</h3>    
  38.                 </div>    
  39.                 <!-- /.card-header -->    
  40.                 <!-- form start -->    
  41.                 <form class="form-horizontal" asp-action="TambahData">    
  42.                         <div class="form-group row">    
  43.                             <label asp-for="Nama_Dokter" class="col-sm-2 col-form-label">Nama Dokter</label>    
  44.                             <div class="col-sm-10">    
  45.                                 @*@Html.EditorFor(model => model.Nama_Dokter)    
  46.                                     @Html.ValidationMessageFor(model => model.Nama_Dokter)*@    
  47.     
  48.                                 <input asp-for="Nama_Dokter" id="Nama_Dokter" class="form-control" placeholder="Nama Dokter">    
  49.                                 <span asp-validation-for="Nama_Dokter" class="text-danger"></span>    
  50.     
  51.                             </div>    
  52.                         </div>    
  53.     
  54.                         <div class="form-group row">    
  55.                             <label asp-for="Alamat" class="col-sm-2 col-form-label">Alamat</label>    
  56.                             <div class="col-sm-10">    
  57.                                 <textarea asp-for="Alamat" id="Alamat" class="form-control" rows="4" placeholder="Alamat"></textarea>    
  58.                                 <span asp-validation-for="Alamat" class="text-danger"></span>    
  59.                             </div>    
  60.                         </div>    
  61.     
  62.                         <div class="form-group row">    
  63.                             <label asp-for="Jenis_Kelamin" class="col-sm-2 col-form-label"> Jenis Kelamin</label>    
  64.                             <div class="col-sm-10">    
  65.                                 @Html.RadioButtonFor(model => model.Jenis_Kelamin, "Laki-Laki") <span>Laki-Laki</span>    
  66.                                 @Html.RadioButtonFor(model => model.Jenis_Kelamin, "Perempuan") <span>Perempuan</span>    
  67.                                 <br />    
  68.                                 <span asp-validation-for="Jenis_Kelamin" id="Jenis_Kelamin" class="text-danger"></span>    
  69.                             </div>    
  70.                         </div>    
  71.     
  72.                         <div class="form-group row">    
  73.                             <label asp-for="Spesialisasi" class="col-sm-2 col-form-label">Spesialisasi</label>    
  74.                             <div class="col-sm-10">    
  75.                                 <input asp-for="Spesialisasi" id="Spesialisasi" class="form-control" placeholder="Spesialisasi">    
  76.                                 <span asp-validation-for="Spesialisasi" class="text-danger"></span>    
  77.                             </div>    
  78.                         </div>    
  79.     
  80.                         <div class="form-group row">    
  81.                             <label asp-for="Keahlian" class="col-sm-2 col-form-label">Keahlian</label>    
  82.                             <div class="col-sm-10">    
  83.                                 <textarea asp-for="Keahlian" id="Keahlian" class="form-control" rows="4" placeholder="Keahlian"></textarea>    
  84.                                 <span asp-validation-for="Keahlian" class="text-danger"></span>    
  85.                             </div>    
  86.                         </div>    
  87.     
  88.                         <div class="form-group row">    
  89.                             <label class="col-sm-2 col-form-label"></label>    
  90.                             <div class="col-sm-10">    
  91.                                 <button type="submit" class="btn btn-success"> Save Data</button>    
  92.                             </div>    
  93.                         </div>    
  94.                 </form>    
  95.             </div>    
  96.             <!-- /.card -->    
  97.             <div>    
  98.                 <a asp-action="Index" class="btn btn-success"><i class="fa fa-arrow-back"> </i> Back to List </a>    
  99.             </div>    
  100.         </div>    
  101.     </section>   
this is the Index.cshtml
  1. @*    
  2.     For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860    
  3. *@    
  4. @*@model AdminLTEMvc.Models.DokterModel*@    
  5. @{    
  6.     Layout = "~/Views/Shared/AdminLayout.cshtml";    
  7. }    
  8. @model System.Data.DataTable    
  9. <link href="~/plugins/fontawesome-free/css/all.css" rel="stylesheet" />    
  10. <!-- Main content -->    
  11. <section class="content">    
  12.     <div class="container-fluid">    
  13.         <div>    
  14.             <a asp-action="TambahData"><i class="fa fa-plus-square btn btn-success"> Tambah Data</i></a>    
  15.         </div>    
  16.         <hr />    
  17.         <table class="table table-bordered table-responsive-lg table-hover" width="100%" id="myTable">    
  18.             <thead class="thead-dark text-center">    
  19.                 <tr>    
  20.                     <th>    
  21.                         Nama Dokter    
  22.                     </th>    
  23.                     <th>    
  24.                         Alamat    
  25.                     </th>    
  26.                     <th>    
  27.                         Jenis Kelamin    
  28.                     </th>    
  29.                     <th>    
  30.                         Spesialisasi    
  31.                     </th>    
  32.                     <th>    
  33.                         Keahlian    
  34.                     </th>    
  35.                     <th>    
  36.                         Action    
  37.                     </th>    
  38.                 </tr>    
  39.             </thead>    
  40.             <tbody class="text-center">    
  41.                 @for (int i = 0; i < Model.Rows.Count; i++)    
  42.                 {    
  43.                     <tr>    
  44.                         <td>    
  45.                             @Model.Rows[i]["Nama_Dokter"]    
  46.                         </td>    
  47.                         <td>    
  48.                             @Model.Rows[i]["Alamat"]    
  49.                         </td>    
  50.                         <td>    
  51.                             @Model.Rows[i]["Jenis_Kelamin"]    
  52.                         </td>    
  53.                         <td>    
  54.                             @Model.Rows[i]["Spesialisasi"]    
  55.                         </td>    
  56.                         <td>    
  57.                             @Model.Rows[i]["Keahlian"]    
  58.                         </td>    
  59.                         <td>    
  60.                             <a asp-action="UpdateData" asp-route-id="@Model.Rows[i]["Id_Dokter"]"><i class="fa fa-edit btn btn-sm btn btn-info"> Edit</i> </a> |    
  61.                             <a asp-action="Delete" asp-route-id="@Model.Rows[i]["Id_Dokter"]"><i class="fa fa-trash btn btn-sm btn btn-danger"> Delete</i> </a>    
  62.                         </td>    
  63.                     </tr>    
  64.                 }    
  65.             </tbody>    
  66.         </table>    
  67.     </div>    
  68.     
  69.     <!-- jQuery -->    
  70.     <script src="~/plugins/jquery/jquery.min.js"></script>    
  71.     <link href="~/plugins/datatables/jquery.dataTables.min.css" rel="stylesheet" />    
  72.     <script src="~/plugins/datatables/jquery.dataTables.js"></script>    
  73.     
  74.     <script type="text/javascript" language="javascript">    
  75.         $(document).ready(function () {    
  76.             $("#myTable").DataTable(    
  77.                 {    
  78.                     bLengthChange: true,    
  79.                     lengthMenu: [[5, 10, 25, 50, 100], [5, 10, 25, 50, 100]],    
  80.                     bFilter: true,    
  81.                     bSort: true,    
  82.                     bPaginate: true    
  83.                 })    
  84.         })    
  85.     </script>    
  86. </section>  

Answers (3)