Thursday, 25 September 2014

JavaScript Alert Message On Conditional Bases In Asp.Net MVC Using C#.Net

9/25/2014 - By Pranav Singh 2

This article will show you how you can display alert message on conditional bases using JavaScript in asp.net mvc application and c#.net. This article we can use in MVC2, MVC3, MVC4,MVC5.


So for this article first we will create a new asp.net mvc application and add the below code into your controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string Type)
        {
            ViewBag.Message = Type;
            return View();
        }

    }
}

In above code please check the httpost attribute method parameter.  In this I have passed a parameter Type this is the dropdown list value which I am storing in viewbag and passing it to view for displaying the value on conditional bases.

Now create view and add the below code in the view.

@{
    ViewBag.Title = "JavaScript Alert Message On conditional Bases In Asp.Net MVC Using C#.Net";
}
<script>
    function Msg1() {
        alert("This is alert message Section - 1");
    }
    function Msg2() {
        alert("This is alert message Section - 2");
    }
</script>
@using (Html.BeginForm("Index", "Home"))
{
   <select name="Type">
   <option value="1">Message 1</option>
   <option value="2">Message 2</option>
   </select>
    <input type="submit" value="Submit"/>
     if (ViewBag.Message == "1")
    {
    <script type>
        Msg1();
    </script>
    }
     else if (ViewBag.Message == "2")
     {
     <script>
         Msg2();
    </script>
     }
}

In above code I have created to simple JavaScript function with alert message, and on the bases of view bag value I am executing the JavaScript function.

Now check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top