Access and show HTML Div Element from ASP.NET CORE MVC C#

Viewed 32

I want to show my bootstrap alert from my C# Controller code.

Currently I am using HTML form and submitting it to my Home Controller. Controller saves data to SQL server and redirect me to the Homepage Index.cshtml.

After saving data I want to show bootstrap confirmation "Data Saved Successfully".

Here is my code

Controller:-Please also suggest If any improvement required in my controller code

        [HttpPost]
        public ActionResult SaveEmp(EmployeesModel _model)
        {
            string CS = this.Configuration.GetConnectionString("SQLConn");
            using (SqlConnection conn = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("SP_SaveEmp", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();

                cmd.Parameters.AddWithValue("@EmployeeID", _model.EmployeeId);
                cmd.Parameters.AddWithValue("@Name", _model.Name);
                cmd.Parameters.AddWithValue("@Gender", _model.Gender);
                cmd.Parameters.AddWithValue("@Age", _model.Age);
                cmd.Parameters.AddWithValue("@Position", _model.Position);
                cmd.Parameters.AddWithValue("@Office", _model.Office);
                cmd.Parameters.AddWithValue("@HiringDate", _model.HiringDate);
                cmd.Parameters.AddWithValue("@Salary", _model.Salary);
                cmd.ExecuteNonQuery();

                conn.Close();
            }

            TempData["AlertMessage"] = _model.Name 
            return RedirectToAction(nameof(Index));
        }

My View

@*Bootstrap Alert*@

<div class="alert alert-success alert-dismissible fade show" style="display:none; padding: 5px" role="alert" runat="server" id="bootstrapAlert">
    <svg class="bi flex-shrink-0 me-1" width="24" height="24" role="img"><use xlink:href="#info-fill" /></svg>
   Records Updated Successfully !
    <button type="button" class="btn-close" aria-hidden="true" style="padding:10px" aria-label="close" onclick="closeAlert()"></button>
</div>


My Form


<div class="row form-group">
<div class="col-md-12 col-md-auto">
@using (Html.BeginForm("SaveEmp", "Home", FormMethod.Post, new { id = "EmpCreateForm" }))
                        {
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary()
                            @Html.TextBox("EmployeeID", "EmployeeID", new { @style = "Display:none;", id = "EmpIDModal" })
                            <span class="form-control-label">@Html.Label("Name")<span style="color:red">* </span></span>
                            @Html.TextBox("Name", null, new { @class = "form-control", id = "namebox" })

}

<button class="btn btn-success btn-rounded" type="submit" id="savebtn" form="EmpCreateForm"><span class="spinner-border-sm"></span>Save</button>

</div>
</div>

Please suggest how do I show my bootstrap alert on data saving ?

PS: ASP.NET CORE doesn't use System.Web.UI

Thanks

0 Answers
Related