Friday, November 18, 2016

How to Show message in MVC

In this article I am going to explain how to show/display message in MVC


Implementation:
We can show message using numerous method. I am going to explain some of them here.

Method 1:
We can display message using Javascript. Check the below given example:

Code of View:
@{
    ViewBag.Title = "Message  Example";
}
<form method="post">
    <input type="button" value="Submit" onclick="message()" />
</form>
   
<script>
    function message() {
        alert('Message : Welcome to Articlemirror');
    }
</script>


Method 2:
Display message using Tempdata. See the below given example:

Controller:
    public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string message)
        {
            message = "Message : Welcome to Articlemirror";
            TempData["Message"] = message;
            return View();
        }

View:
@{
    ViewBag.Title = "Message  Example";
}
<form method="post">
    <div>@TempData["Message"]</div>
    <input type="submit" value="Submit" value="Submit" />

</form>



Method 3:
Display message using Viewbag. See the below given example:

Controller:
   public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string message)
        {
            message = "Message : Welcome to Articlemirror";
            ViewBag.Message = message;
            return View();
        }

View:

@{
    ViewBag.Title = "Message  Example";
}
<form method="post">
    <div>@ViewBag.Message</div>
    <input type="submit" value="Submit" value="Submit" /> 
</form>

Or we can also display message in alert/pop up box.

@{
    ViewBag.Title = "Message  Example";
}
<input type="button" value="Submit" onclick="message()" />
<script>
    function message() {
        alert('@ViewBag.Message');
    }
</script>


Hope this article help you!

No comments:

Post a Comment