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.
Some of my previous articles are as follows: Javascript
Confirm Message On Submit Button Click In Asp.Net MVC Using C#.Net, Dynamic
Histogram Google Chart in asp.net MVC using C#.net, JavaScript, Dynamic
Google Geo Chart In an Asp.Net MVC Using C# and Javascript, Gridview
Selected Row Value Pass to JavaScript Function On Hyperlink Click in Asp.net
Using C#.Net, Access
Session Value at Client Side Using JavaScript In Asp.Net.
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.
nice post
ReplyDeletenice post
ReplyDelete