Showing posts with label jQuery DialogBox. Show all posts
Showing posts with label jQuery DialogBox. Show all posts

jQuery DialogBox with Form in MVC3 Razor



  1. In this article we will see how to render a form inside a Dialog box.
  2. We often come across a requirement where we need to render a form inside a popup or modal box.
  3. We can use the jQuery Modal Dialog Box to accomplish this.
  4. We are rendering a form inside the modal dialog box.
  5. We are also validating the user input.
  6. We have integrated jQuery dialog box in MVC3 Razor application.


Following are the steps :

View :



@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

<h2>Index</h2>

<div id="UserTable">
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
<tr>
<td>Jack Wilshere</td>
<td>Jack@gmail.com</td>
<td>Wlshere</td>
</tr>
</table>
<input type="button" id="createUser" value="Create User" />
</div>

<div id="dialogForm" title="Create User">
<span id="warningSpan"></span><br /><br />
<form>
<label for="name">Name</label><br />
<input type="text" name="name" id="name" /><br />
<label for="email">Email</label><br />
<input type="text" name="email" id="email" /><br />
<label for="password">Password</label><br />
<input type="text" name="password" id="password" /><br />
</form>
</div>


<script type="text/javascript">
    $(function () {
        $("#dialogForm").dialog({
            autoOpen: false,
            height: 400,
            width: 350,
            modal: true,
            buttons: {
                "Create User": function () {
                    createNewUser();
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#createUser")
   .button()
   .click(function () {
       $("#dialogForm").dialog("open");
   });
    });

    function createNewUser() {
        var nameValue = $("#name").val();
        var emailValue = $("#email").val();
        var passwordValue = $("#password").val();
        $("#warningSpan").text('');
        var isValid = validateForm(nameValue, emailValue, passwordValue);
        debugger;
        if (isValid) {
            $("#UserTable tbody").append("<tr>" +
       "<td>" + nameValue + "</td>" +
       "<td>" + emailValue + "</td>" +
       "<td>" + passwordValue + "</td>" +
      "</tr>");
            $("#dialogForm").dialog("close");
        $("#name").val("");
        $("#email").val("");
        $("#password").val("");
        }
    }

    function validateForm(name, email, password) {

        var isValidName = true;
        var isValidEmail = true;
        var isValidPassword = true;

        if (name == '') {
            $("#warningSpan").text("name is mandatory!");
            isValidName = false;
        }
        if (email == '') {
            var warningSpan = $("#warningSpan").text();
            if (warningSpan != null) {
                $("#warningSpan").append("<br /> email is mandatory!");
            }
            else {
                $("#warningSpan").text("email is mandatory!");
            }
            isValidEmail = false;
        }
        else {
            var regex = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            if (regex.test(email)) {
                isValidEmail = true;
            }
            else {
                isValidEmail = false;
                var warningSpan = $("#warningSpan").text();
                if (warningSpan != null) {
                    $("#warningSpan").append("<br /> email is not in correct format!");
                }
                else {
                    $("#warningSpan").text("email is not in correct format!");
                }
            }
        }
        if (password == '') {
            var warningSpan = $("#warningSpan").text();
            if (warningSpan != null) {
                $("#warningSpan").append("<br/> password is mandatory!");
            }
            else {
                $("#warningSpan").text("password is mandatory!");
            }
            isValidPassword = false;
        }
        if (isValidName && isValidEmail && isValidPassword) {
            return true;
        }
        else {
            return false;
        }
    }
</script>

In the View we have referenced the Layout, which has jQuery script file references needed to render jQuery Dialog box. In the above view, we have created a form and a table. The form is rendered inside a Dialog box.
       We have applied the validation on Form input fields. When all the validations are passed the record is added to the Grid.


Layout :


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.dialog.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.widget.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.bgiframe-2.1.2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.draggable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.position.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.resizable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.effect.min.js")" type="text/javascript"></script>
</head>
 
<body>
    @RenderBody()
</body>
</html>

The above is the layout file. We have referenced the jQuery files needed to make dialog box work.

Demo :





In the above demo, when you click on Create User button, a dialog box appears which is modal. We can enter details and click Create user to enter user details in the table. We have also applied validations to the form rendered inside modal dialog box.
                            Thus by following above steps we can render a form inside dialog box. We can also customize this even more to achieve more functionalities with jQuery Dialog box.

Modal Dialog box with confirmation in MVC3 Razor



  1. In this article we will see how to create a Modal Dialog box with confirmation. 
  2. We can use this confirmation to notify before performing any critical operations.
  3. We can confirm an action that may be destructive or important. We have set the modal option to true, and specified primary and secondary user actions with the buttons option.


Following are the steps :

View :

@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

<h2>Index</h2>

<div id="dialog" title="jQuery Dialog"><span>Delete All Items !!</span></div>


<script type="text/javascript">
    $(function () {
        $("#dialog").dialog({
            resizable: false,
            height: 140,
            modal: true,
            buttons: {
                "Delete all items": function () {
                    $(this).dialog("close");
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
    });
</script>

In the view we have added reference of layout which contains references of script files. We have created two buttons which will act as a confirmation point. On click of these buttons we can perform various other operations as well.

Layout :



<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.dialog.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.widget.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.bgiframe-2.1.2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.draggable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.position.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.resizable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.effect.min.js")" type="text/javascript"></script>
</head>

<body>
    @RenderBody()
</body>
</html>

The above is the Layout file. We have included the script file reference required to render the Dialog box.

Demo :





Thus we have rendered two buttons. The above dialog box act as a confirmation. We can perform different operation on click of the buttons.

Thus we can use jQuery dialog box to act as confirmatio.

Modal Dialog box in MVC3 Razor



  1. In this article we will see how to render Model Dialog box.
  2. A modal dialog prevents the user from interacting with the rest of the page until it is closed.

Following are the steps :

View :

@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

<h2>Index</h2>

<div id="dialog" title="jQuery Dialog"><p>Hello World !!</p><p>This is jQuery DialogBox !!</p></div>


<script type="text/javascript">
    $(function () {
        $("#dialog").dialog({
            height: 200,
            modal: true
        });
    });
</script>

In the view, we have referenced Layou which have script files reffered needed to render Dialog box. We have set modal property to true to render the modal dialog box. We have also set the height attribute.

Layout :




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.dialog.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.widget.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.bgiframe-2.1.2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.draggable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.position.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.resizable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.effect.min.js")" type="text/javascript"></script>
</head>

<body>
    @RenderBody()
</body>
</html>

We have included the jQuery references needed to render Dialog Box.

Demo :





In the above demo, unless you close the modal you won't be able to access or click other elements of screen. 

Thus following the above steps we can achieve rendering Modal dialog box.

Dynamic jQuery DialogBox in MVC3 Razor with animation



  1. In this article we will see how to render a Dynamic jQuery DialogBox.
  2. The Dialogbox will open on button click.
  3. We have applied animation while opening and closing of DialogBox.
  4. Dialogs may be animated by specifying an effect for the show and/or hide properties. You must include the individual effects file for any effects you would like to use.
  5. We have used the jQuery Dialogbox in MVC3 Razor.


Following are the setps :

View :

@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

<h2>Index</h2>
<input type="button" id="opener" value="Open Dialog" />
<div id="dialog" title="jQuery Dialog"><p>Hello World !!</p><p>This is jQuery DialogBox !!</p></div>


<script type="text/javascript">
    $.fx.speeds._default = 1000;
    $(function () {
        $("#dialog").dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode"
        });

        $("#opener").click(function () {
            $("#dialog").dialog("open");
            return false;
        });
    });
</script>

In the View we have referred the Layout. This layout contains reference to the jQuery script files needed for jQuery DialogBox and animation. We have created a button with id attribute as opener. On click of this button, the Dialog box is opened. We have set the autoOpen attribute to false, so that Dialog box is not open by default. We have used blind animation while showing and explode animation while hiding the DialogBox.

Layout :



<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.dialog.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.widget.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.bgiframe-2.1.2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.draggable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.position.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.resizable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.effect.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.effect-blind.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.effect-explode.min.js")" type="text/javascript"></script>
</head>

<body>
    @RenderBody()
</body>
</html>

The above layout file contains references of script files required to render jQuery dialog box and animations performed on it.

Demo :





In the above demo, when you click the button the dialog box opens. We have applied animation to dialog box. The animation is performed while opening and closing of Dialog box.

Thus following the above steps we can use Dialog box in MVC3 Razor and use various animations on it.

jQuery DialogBox in MVC3 Razor



  1. In this article we will see how to use jQuery DialogBox to render a Dialog Box.
  2. The basic dialog window is an overlay positioned within the viewport and is protected from page content (like select elements) shining through with an iframe. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default.
  3. We have used jQuery Dialogbox in MVC3 Razor application.

Following are the steps :

View :


@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
}

<h2>Index</h2>

<div id="dialog" title="jQuery Dialog"><p>Hello World !!</p><p>This is jQuery DialogBox !!</p></div>


<script type="text/javascript">
    $(function () {
        $("#dialog").dialog();
    });
</script>

In the view, we have included reference of Layout where we have included all the reference of jQuery script files. We have created a div with id dialog. In the script section, we have selected the div element using id selector and used dialog function to render the dialog box.

Layout :



<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.dialog.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.widget.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.bgiframe-2.1.2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.draggable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.position.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.resizable.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
</head>


<body>
    @RenderBody()
</body>
</html>

The above is the Layout file. We need to include above jQuery script files to achieve jQuery Dialogbox rendering.

Demo :




Thus following the above steps we can render the Dialog box successfully in MVC3 Razor.