Getting Started with ASP.NET MVC Dialog Control
8 Nov 202313 minutes to read
This section briefly explains about how to include ASP.NET MVC Dialog control in your ASP.NET MVC application using Visual Studio.
Prerequisites
System requirements for ASP.NET MVC controls
Create ASP.NET MVC application with HTML helper
Install ASP.NET MVC package in the application
To add ASP.NET MVC
controls in the application, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for Syncfusion.EJ2.MVC5 and then install it.
Install-Package Syncfusion.EJ2.MVC5 -Version 27.2.2
NOTE
Syncfusion ASP.NET MVC controls are available in nuget.org. Refer to NuGet packages topic to learn more about installing NuGet packages in various OS environments. The Syncfusion.EJ2.MVC5 NuGet package has dependencies, Newtonsoft.Json for JSON serialization and Syncfusion.Licensing for validating Syncfusion license key.
NOTE
If you create ASP.NET MVC application with MVC4 package, search for Syncfusion.EJ2.MVC4 and then install it.
Add namespace
Add Syncfusion.EJ2 namespace reference in Web.config
under Views
folder.
<namespaces>
<add namespace="Syncfusion.EJ2"/>
</namespaces>
Add stylesheet and script resources
Here, the theme and script is referred using CDN inside the <head>
of ~/Pages/Shared/_Layout.cshtml
file as follows,
<head>
...
<!-- Syncfusion ASP.NET MVC controls styles -->
<link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/27.2.2/fluent.css" />
<!-- Syncfusion ASP.NET MVC controls scripts -->
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js"></script>
</head>
NOTE
Checkout the Themes topic to learn different ways (CDN, NPM package, and CRG) to refer styles in ASP.NET MVC application, and to have the expected appearance for Syncfusion ASP.NET MVC controls. Checkout the Adding Script Reference topic to learn different approaches for adding script references in your ASP.NET MVC application.
Register Syncfusion script manager
Also, register the script manager EJS().ScriptManager()
at the end of <body>
in the ~/Pages/Shared/_Layout.cshtml
file as follows.
<body>
...
<!-- Syncfusion ASP.NET MVC Script Manager -->
@Html.EJS().ScriptManager()
</body>
Add ASP.NET MVC Dialog control
Now, add the Syncfusion ASP.NET MVC Dialog control in ~/Views/Home/Index.cshtml
page.
<div id="container" style="height:400px;">
@Html.EJS().Button("targetButton").Content("Open Dialog").Render()
@Html.EJS().Dialog("dialog").Header("Dialog").Content("This is a Dialog with content").Target("#container").Width("250px").Render()
</div>
<script>
window.onload = function () {
document.getElementById('targetButton').onclick = function () {
var dialog = document.getElementById("dialog").ej2_instances[0]
dialog.show();
}
}
</script>
Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to run the app. Then, the Syncfusion ASP.NET MVC Dialog control will be rendered in the default web browser.
NOTE
In the dialog control, max-height is calculated based on the dialog target element height. If the target property is not configured, the document.body is considered as a target. Therefore, to show a dialog in proper height, you need to add min-height to the target element.
Modal Dialog
A modal shows an overlay behind the Dialog. So, the user should interact the Dialog compulsory before interacting with the remaining content in an application.
While the user clicks the overlay, the action can be handled through the OverlayClick event. In the below sample, the Dialog close action is performed while clicking on the overlay.
NOTE
When the modal dialog is opened, the Dialog’s target scrolling will be disabled. The scrolling will be enabled again once close the Dialog.
<div id='container' style="height:400px;">
@Html.EJS().Button("targetButton").Content("Open Modal Dialog").Render()
@Html.EJS().Dialog("dialog").IsModal(true).OverlayClick("onOverlayClick").Content("This is a modal dialog").Width("300px").Target("#container").Render()
</div>
<script>
window.onload = function () {
document.getElementById('targetButton').onclick = function () {
var dialog = document.getElementById("dialog").ej2_instances[0];
dialog.show();
}
}
function onOverlayClick() {
var dialog = document.getElementById("dialog").ej2_instances[0];
dialog.hide();
}
</script>
NOTE
In the dialog control, If the dialog is rendered based on the body, then the dialog get the height is based on its body element height. If the height of the dialog is larger than the body height, then the dialog’s height will not be set. For this scenario, we can set the CSS style for the html and body to get the dialog height.
html, body {
height: 100%;
}
Enable header
The Dialog header can be enabled by adding the header content as text or HTML content through the Header property.
<div id='container' style="height:400px;">
@Html.EJS().Button("targetButton").Content("Open Dialog").Render()
@Html.EJS().Dialog("dialog").Header("Dialog").ShowCloseIcon(true).Content("This is a dialog with header").Target("#container").Width("250px").Render()
</div>
<script>
window.onload = function () {
document.getElementById('targetButton').onclick = function () {
var dialog = document.getElementById("dialog").ej2_instances[0];
dialog.show();
}
}
</script>
Enable footer
The Dialog provides built-in support to render the buttons
on the footer (for ex: OK
or Cancel
buttons). Each Dialog button allows the user to perform any action while clicking on it.
The primary button will be focused automatically on open the Dialog, and add the Click event to handle the actions.
NOTE
When the Dialog initialize with more than one primary buttons, the first primary button gets focus on open the Dialog.
The below sample render with button and its click event.
<div id="container" style="height:400px;">
@Html.EJS().Button("targetButton").Content("Open Dialog").Render()
@Html.EJS().Dialog("dialog").Header("Dialog").Content("This is a Dialog with button and primary button").Target("#container").Width("250px").Buttons(btn=> {
btn.Click("dlgButtonClick").ButtonModel(ViewBag.DialogButtons1).Add();
btn.Click("dlgButtonClick").ButtonModel(ViewBag.DialogButtons2).Add();
}).Render()
</div>
<script>
window.onload = function () {
document.getElementById('targetButton').onclick = function () {
var dialog = document.getElementById("dialog").ej2_instances[0];
dialog.show();
}
}
function dlgButtonClick() {
var dialogObj = document.getElementById('dialog').ej2_instances[0];
dialogObj.hide();
}
</script>
public class HomeController : Controller
{
public class ButtonModel
{
public string content { get; set; }
public bool isPrimary { get; set; }
public string cssClass { get; set; }
}
public ActionResult Index()
{
ViewBag.DialogButtons1 = new ButtonModel() { isPrimary = true, cssClass = "e-flat", content = "OK" };
ViewBag.DialogButtons2 = new ButtonModel() { content = "Cancel", cssClass = "e-flat" };
return View();
}
}
Draggable
The Dialog supports to drag within its target container by grabbing the Dialog header, which allows the user to reposition the Dialog dynamically.
NOTE
The Dialog can be draggable only when the Dialog header is enabled. From
16.2.x
version, enabled draggable support for modal dialog also.
<div id='container' style="height:400px;">
@Html.EJS().Button("targetButton").Content("Open Dialog").Render()
@Html.EJS().Dialog("dialog").Header("Dialog").Content("This is a Dialog with drag enabled").AllowDragging(true).Target("#container").Width("250px").AllowDragging(true).Buttons(btn=> {
btn.Click("dlgButtonClick").ButtonModel(ViewBag.DialogButtons1).Add();
btn.Click("dlgButtonClick").ButtonModel(ViewBag.DialogButtons2).Add();
}).Render()
</div>
<script>
window.onload = function () {
document.getElementById('targetButton').onclick = function () {
var dialog = document.getElementById("dialog").ej2_instances[0];
dialog.show();
}
}
function dlgButtonClick() {
var dialogObj = document.getElementById('dialog').ej2_instances[0];
dialogObj.hide();
}
</script>
public class HomeController : Controller
{
public class ButtonModel
{
public string content { get; set; }
public string cssClass { get; set; }
}
public ActionResult Index()
{
ViewBag.DialogButtons1 = new ButtonModel() { cssClass = "e-flat", content = "OK" };
ViewBag.DialogButtons2 = new ButtonModel() { content = "Cancel", cssClass = "e-flat" };
return View();
}
}
Positioning
The Dialog position can be set through the Position property by providing X and Y coordinates. The Dialog can be positioned inside the target container based on the given X and Y values.
For example position:{ X:'center', Y:'center' }
the possible values.
- for X is: left, center, right (or) any offset value
- for Y is: top, center, bottom (or) any offset value
The below sample demonstrates the different Dialog positions.
<div id='container' style="height:400px;">
@Html.EJS().Button("targetButton").Content("Open Dialog").Render()
@Html.EJS().Dialog("dialog").Header("Dialog Positions").FooterTemplate("<span id='posvalue' style='float:left; padding-left:10px;font-weight: bold;'>Position: {X: 'left', Y: 'top'}</span>").Target("#container").Width("350px").CloseOnEscape(false).ContentTemplate(@<table style='width: 320px;'>
<tr> <td><input type='radio' name='xy' onclick='changePosition(event)' value='left top' checked='true'>left top</td> <td><input type='radio' name='xy' onclick='changePosition(event)' value='center top'>center top</td> <td><input type='radio' name='xy' onclick='changePosition(event)' value='right top'>right top</td> </tr>
<tr> <td><input type='radio' name='xy' onclick='changePosition(event)' value='left center'>left center</td> <td><input type='radio' name='xy' onclick='changePosition(event)' value='center center'>center center</td> <td><input type='radio' name='xy' onclick='changePosition(event)' value='right center'>right center</td> </tr>
<tr> <td><input type='radio' name='xy' onclick='changePosition(event)' value='left bottom'>left bottom</td> <td><input type='radio' name='xy' onclick='changePosition(event)' value='center bottom'>center bottom</td> <td><input type='radio' name='xy' onclick='changePosition(event)' value='right bottom'>right bottom</td> </tr>
</table>).Render()
</div>
<script>
function changePosition(event) {
var dialog = document.getElementById("dialog").ej2_instances[0];
dialog.position = { X: event.currentTarget.value.split(" ")[0], Y: event.currentTarget.value.split(" ")[1] };
document.getElementById("posvalue").innerHTML = 'Position: {X: "' + event.currentTarget.value.split(" ")[0] + '", Y: "' + event.currentTarget.value.split(" ")[1] + '"}';
};
</script>
NOTE