Monday, 27 November 2017

Display Success Message after Form Submit (Post) or Record Insert in ASP.Net MVC

Display Success Message after Form Submit (Post) or Record Insert in ASP.Net MVC

Description:

In this example we explain that how to Display Success Message after Record Insert in ASP.Net MVC.or how to display success message to user after successfully inserted record in MVC.or how to display success message after form submit in asp.net MVC.or display success message to user when the form is submitted. Or display success message on the same page when the form is submitted in MVC.or display (Show) success message after record is inserted in MVC.
Model:

public class EmployeeModel
    {

        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
    }

 Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(EmployeeModel emp)
        {
            string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "INSERT INTO Employee(Name, Country) VALUES(@Name, @Country)";
                query += " SELECT SCOPE_IDENTITY()";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    con.Open();
                    cmd.Parameters.AddWithValue("@Name", emp.Name);
                    cmd.Parameters.AddWithValue("@Country", emp.Country);
                    emp.EmployeeId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                }
            }

            return View(emp);
        }
   
    }

View: 

@model ADO_Net_MVC.Models.EmployeeModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Display Success Message after Form Submit (Post) or Record Insert in ASP.Net MVC</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Employee Details</th>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Country: </td>
                <td>
                   @Html.DropDownListFor(m => m.Country, new List<SelectListItem>
                   { new SelectListItem{Text="India", Value="India"},
                     new SelectListItem{Text="China", Value="China"},
                     new SelectListItem{Text="Australia", Value="Australia"},
                     new SelectListItem{Text="France", Value="France"},
                     new SelectListItem{Text="Unites States", Value="Unites States"},
                     new SelectListItem{Text="Russia", Value="Russia"},
                     new SelectListItem{Text="Canada", Value="Canada"}},
                     "Please select")
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    }

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    @if (Model != null)
    {
        <script type="text/javascript">
            $(function () {
                alert("Inserted Employee ID: " + @Model.EmployeeId);
            });
        </script>
    }
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment