How to Show message in MVC - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

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!

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon