2

I would like to popup a messagebox to notify user with a success message after submitting the form.

I have the following Controller and View.

 public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(PersonModel person)
        {
            int personId = person.PersonId;
            string name = person.Name;
            string gender = person.Gender;
            string city = person.City;

            return View();
        }
    }

View -

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }

        table {
            border: 1px solid #ccc;
            border-collapse: collapse;
            background-color: #fff;
        }

        table th {
            background-color: #B8DBFD;
            color: #333;
            font-weight: bold;
        }

        table th, table td {
            padding: 5px;
            border: 1px solid #ccc;
        }

        table, table table td {
            border: 0px solid #ccc;
        }

        input[type=text], select {
            width: 150px;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Person Details</th>
            </tr>
            <tr>
                <td>PersonId: </td>
                <td>
                    @Html.TextBoxFor(m => m.PersonId)
                </td>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td>
                    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
                   { new SelectListItem{Text="Male", Value="M"},
                     new SelectListItem{Text="Female", Value="F"}}, "Please select")
                </td>
            </tr>
            <tr>
                <td>City: </td>
                <td>
                    @Html.TextBoxFor(m => m.City)
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    }
</body>
</html>
s.k.paul
  • 7,099
  • 28
  • 93
  • 168
MilkBottle
  • 4,242
  • 13
  • 64
  • 146

1 Answers1

0

You can simply use ViewBag and Javascript for this.

In your controller:

public ActionResult Index()
{
    ViewBag.SuccessMessage = "";
    return View();
}

[HttpPost]
public ActionResult Index(PersonModel person)
{
    int personId = person.PersonId;
    string name = person.Name;
    string gender = person.Gender;
    string city = person.City;

    ViewBag.SuccessMessage = "Data has been Inserted!"

    return View();
}

Add a script in the end of your body,

<script>
    $(document).ready(function() {
        if(@ViewBag.SuccessMessage != ""){
            alert(@ViewBag.SuccessMessage);
        }
    });
</script>
kevaljarsania
  • 127
  • 2
  • 8