TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tri Setia
1.3k
464
24.4k
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
using
Microsoft.AspNetCore.Mvc;
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Threading.Tasks;
using
AdminLTEMvc.Models;
using
Microsoft.Extensions.Configuration;
using
System.Data;
using
Microsoft.Data.SqlClient;
using
Microsoft.Data;
namespace
AdminLTEMvc.Controllers
{
public
class
DokterController : Controller
{
private
readonly IConfiguration _configuration;
public
DokterController(IConfiguration configuration)
{
this
._configuration = configuration;
}
public
IActionResult Index()
{
DataTable dt =
new
DataTable();
using
(SqlConnection con =
new
SqlConnection(_configuration.GetConnectionString(
"db_klinik"
)))
{
con.Open();
SqlCommand cmd =
new
SqlCommand(
"Select * From Dokter"
, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adap =
new
SqlDataAdapter(cmd);
adap.Fill(dt);
}
return
View(dt);
}
[HttpGet]
public
IActionResult TambahData()
{
ViewBag.pesan = TempData[
"pesan"
] as string;
return
View();
}
public
IActionResult TambahData(DokterModel dokterModel)
{
if
(ModelState.IsValid)
{
using
(SqlConnection con =
new
SqlConnection(_configuration.GetConnectionString(
"db_klinik"
)))
{
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))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
TempData[
"pesan"
] =
"Data Berhasil disimpan..."
;
return
RedirectToAction(
"Index"
,
"Dokter"
);
}
}
}
else
{
return
View(dokterModel);
}
}
}
}
This is the TambahData.cshtml
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model AdminLTEMvc.Models.DokterModel
@{
Layout =
"~/Views/Shared/AdminLayout.cshtml"
;
}
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>Tambah Data Dokter</title>
<script src=
"~/plugins/jquery/jquery.min.js"
></script>
<script src=
"~/lib/jquery-validation/dist/jquery.validate.min.js"
></script>
<script src=
"~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
></script>
<link href=
"~/dist/css/adminlte.min.css"
rel=
"stylesheet"
/>
<script src=
"~/plugins/bootstrap/js/bootstrap.min.js"
></script>
<script src=
"~/plugins/sweetalert2/sweetalert2.js"
></script>
<link href=
"~/plugins/sweetalert2/sweetalert2.css"
rel=
"stylesheet"
/>
<script type=
"text/javascript"
>
var
Pesan =
"@((string)ViewBag.pesan)"
;
if
(strvalue !==
null
&& strvalue !==
''
)
{
swal(
"Good job!"
,Pesan,
"success"
)
};
</script>
</head>
<body>
<!-- Main content -->
<section
class
=
"content"
>
<div
class
=
"container-fluid"
>
<!-- Horizontal Form -->
<div
class
=
"card card-green"
>
<div
class
=
"card-header"
>
<h3
class
=
"card-title"
>Tambah Data Dokter</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form
class
=
"form-horizontal"
asp-action=
"TambahData"
>
<div
class
=
"form-group row"
>
<label asp-
for
=
"Nama_Dokter"
class
=
"col-sm-2 col-form-label"
>Nama Dokter</label>
<div
class
=
"col-sm-10"
>
@*@Html.EditorFor(model => model.Nama_Dokter)
@Html.ValidationMessageFor(model => model.Nama_Dokter)*@
<input asp-
for
=
"Nama_Dokter"
id=
"Nama_Dokter"
class
=
"form-control"
placeholder=
"Nama Dokter"
>
<span asp-validation-
for
=
"Nama_Dokter"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group row"
>
<label asp-
for
=
"Alamat"
class
=
"col-sm-2 col-form-label"
>Alamat</label>
<div
class
=
"col-sm-10"
>
<textarea asp-
for
=
"Alamat"
id=
"Alamat"
class
=
"form-control"
rows=
"4"
placeholder=
"Alamat"
></textarea>
<span asp-validation-
for
=
"Alamat"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group row"
>
<label asp-
for
=
"Jenis_Kelamin"
class
=
"col-sm-2 col-form-label"
> Jenis Kelamin</label>
<div
class
=
"col-sm-10"
>
@Html.RadioButtonFor(model => model.Jenis_Kelamin,
"Laki-Laki"
) <span>Laki-Laki</span>
@Html.RadioButtonFor(model => model.Jenis_Kelamin,
"Perempuan"
) <span>Perempuan</span>
<br />
<span asp-validation-
for
=
"Jenis_Kelamin"
id=
"Jenis_Kelamin"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group row"
>
<label asp-
for
=
"Spesialisasi"
class
=
"col-sm-2 col-form-label"
>Spesialisasi</label>
<div
class
=
"col-sm-10"
>
<input asp-
for
=
"Spesialisasi"
id=
"Spesialisasi"
class
=
"form-control"
placeholder=
"Spesialisasi"
>
<span asp-validation-
for
=
"Spesialisasi"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group row"
>
<label asp-
for
=
"Keahlian"
class
=
"col-sm-2 col-form-label"
>Keahlian</label>
<div
class
=
"col-sm-10"
>
<textarea asp-
for
=
"Keahlian"
id=
"Keahlian"
class
=
"form-control"
rows=
"4"
placeholder=
"Keahlian"
></textarea>
<span asp-validation-
for
=
"Keahlian"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group row"
>
<label
class
=
"col-sm-2 col-form-label"
></label>
<div
class
=
"col-sm-10"
>
<button type=
"submit"
class
=
"btn btn-success"
> Save Data</button>
</div>
</div>
</form>
</div>
<!-- /.card -->
<div>
<a asp-action=
"Index"
class
=
"btn btn-success"
><i
class
=
"fa fa-arrow-back"
> </i> Back to List </a>
</div>
</div>
</section>
this is the Index.cshtml
@*
For more information on enabling MVC
for
empty projects, visit https:
//go.microsoft.com/fwlink/?LinkID=397860
*@
@*@model AdminLTEMvc.Models.DokterModel*@
@{
Layout =
"~/Views/Shared/AdminLayout.cshtml"
;
}
@model System.Data.DataTable
<link href=
"~/plugins/fontawesome-free/css/all.css"
rel=
"stylesheet"
/>
<!-- Main content -->
<section
class
=
"content"
>
<div
class
=
"container-fluid"
>
<div>
<a asp-action=
"TambahData"
><i
class
=
"fa fa-plus-square btn btn-success"
> Tambah Data</i></a>
</div>
<hr />
<table
class
=
"table table-bordered table-responsive-lg table-hover"
width=
"100%"
id=
"myTable"
>
<thead
class
=
"thead-dark text-center"
>
<tr>
<th>
Nama Dokter
</th>
<th>
Alamat
</th>
<th>
Jenis Kelamin
</th>
<th>
Spesialisasi
</th>
<th>
Keahlian
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody
class
=
"text-center"
>
@
for
(
int
i = 0; i < Model.Rows.Count; i++)
{
<tr>
<td>
@Model.Rows[i][
"Nama_Dokter"
]
</td>
<td>
@Model.Rows[i][
"Alamat"
]
</td>
<td>
@Model.Rows[i][
"Jenis_Kelamin"
]
</td>
<td>
@Model.Rows[i][
"Spesialisasi"
]
</td>
<td>
@Model.Rows[i][
"Keahlian"
]
</td>
<td>
<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> |
<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>
</td>
</tr>
}
</tbody>
</table>
</div>
<!-- jQuery -->
<script src=
"~/plugins/jquery/jquery.min.js"
></script>
<link href=
"~/plugins/datatables/jquery.dataTables.min.css"
rel=
"stylesheet"
/>
<script src=
"~/plugins/datatables/jquery.dataTables.js"
></script>
<script type=
"text/javascript"
language=
"javascript"
>
$(document).ready(
function
() {
$(
"#myTable"
).DataTable(
{
bLengthChange:
true
,
lengthMenu: [[5, 10, 25, 50, 100], [5, 10, 25, 50, 100]],
bFilter:
true
,
bSort:
true
,
bPaginate:
true
})
})
</script>
</section>
Reply
Answers (
3
)
check date with javascript
12 factor, containerized applications to AWS