Hi Guys,,
I'm trying to make alert message for end user. I want is when end user typed wrong code it will show the alert message like "Data not found !!!".
I'm try to show the alert using sweet alert, but unfortunately the alert not appears.
Create.cshtml
<script type="text/javascript" lang="=javascript">
$(function () {
if ('@ViewBag.msg' != "") {
$('#myModal').modal({ show: true });
}
});
</script>
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
$("#CekNamaAnggota").click(function () {
$.ajax({
url: "@Url.Action("SearchNamaAnggota", "Pinjaman")",
type:"POST",
data: { Kode_Anggota: $("#txtKodeAnggota").val() },
success: function (data) {
$("#txtNamaAnggota").val(data);
}
});
});
});
</script>
Controller.cs
//retrieve data from database
[HttpPost]
public IActionResult SearchNamaAnggota(string Kode_Anggota)
{
string Nama = "";
using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
{
using (SqlCommand cmd = new SqlCommand())
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select Nama From Anggota Where Kode_Anggota =@Kode_Anggota";
cmd.Parameters.AddWithValue("@Kode_Anggota", Kode_Anggota);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
if (rdr.Read())
{
Nama = rdr["Nama"].ToString();
}
else
{
TempData["pesan"] = "Kode Anggota not found !!!";
return RedirectToAction(nameof(Create));
}
}
con.Close();
}
}
return Json(Nama);
}