Open Dialog from controller

Hi,
Can I open a dialog from the controller to see the errors of a try catch block?

3 Replies

AP Arun Palaniyandi Syncfusion Team January 19, 2018 11:20 AM UTC

 
Hi Pio Luca Valvona,   
 
Thanks for contacting Syncfusion support.   
   
Yes, you can open a dialog from the controller to see the errors of a try catch block. First, we have to create the wrapper for dialog control in view page and set the property showOnInit as false in controller. Initially it doesn’t show the dialog. Then based on our condition you need to enable this property as true in the controller and put your error message in Dialog content template. Now the dialog control get displays with the thrown error message in view. Please find the below code snippets.   
  
CSHTML 
 
<form id="form1" method="post"> 
 
    <div style="display:none"> 
    @{ 
     
    Html.EJ().Dialog("basicDialog",Model).Render();   // render the Dialog in view  
 
    } 
 
        </div> 
    <div> 
 
        @Html.EJ().Button("btnOpen").Type(ButtonType.Submit).Text("Click to check").ClientSideEvents(evt => evt.Click("onclick"))   
 
    </div> 
</form> 
 
CONTROLLER 
 
public ActionResult Index() 
        { 
            Syncfusion.JavaScript.Models.DialogProperties dia = new Syncfusion.JavaScript.Models.DialogProperties(); 
            dia.ShowOnInit = false// initially set as false 
             
            return View(dia); 
        } 
 
[HttpPost] 
        public ActionResult Index(string basicDialog) 
        { 
 
            int a = 1; 
            int b = 0; 
            int c = 0;   
 
            try 
            { 
                c = a / b;  //try the code here 
            } 
            catch (Exception ex) 
            { 
 
                Syncfusion.JavaScript.Models.DialogProperties dia = new Syncfusion.JavaScript.Models.DialogProperties(); 
                dia.ShowOnInit = true; 
                dia.Title = "Error Message"; 
                dia.ContentTemplate = new MvcTemplate<object> 
                { 
                    RazorViewTemplate = (data) => 
                    { 
                        return ex; // return the error message in the Dialog content template like this  
                    } 
                }; 
                return View(dia);               
 
            } 
            return View(); 
 
 
        } 
    } 
 
 
We have also prepared a sample below for your reference. 

Please let us know if the provided information’s are helpful to achieve your requirement or not.  
 
Regards, 
Arun P. 





PL Pio Luca Valvona January 20, 2018 02:40 PM UTC

Hi Arun,
thanks for support, but it not works. Only Messagge in content template shows in view but dialog not show.

Partial View (with dialog)
@model PLV.Web.Models.ViewModels.LoginVM
<div id="dvform">
    @{Html.EJ().Dialog("dialog", (Syncfusion.JavaScript.Models.DialogProperties)ViewBag.Dia).Render(); }
    @using (Ajax.BeginForm("Index""Login"new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "dvform" }))
    {
        @Html.AntiForgeryToken()
        <div class="col-lg-6 col-sm-6 col-lg-offset-3 col-sm-offset-3">
            <div class="row">
                <div class="form-group ">
                    @Html.LabelFor(m => m.Codice)
                    @Html.EditorFor(m => m.Codice, new { htmlattributes = new { @class = "form-control input-lg" } })
                    @Html.ValidationMessageFor(m => m.Codice, ""new { @class = "text-danger" })
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password)
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control input-lg" })
                    @Html.ValidationMessageFor(m => m.Password, ""new { @class = "text-danger" })
                </div>
                <hr />
                <input type="submit" value="@PLV.Web.Resources.Login.Accedi" class="buttoncolor" />
                <a rel='nofollow' href="@Url.Action("RecuperoPassword","Login")" class="buttonyellow">&nbsp; @(PLV.Web.Resources.Login.RecuperaPassword)</a>
 
                @if (TempData["error"!= null)
                {
                    <div class="alert alert-danger" role="alert">@TempData["error"]</div>
                }
            </div>
        </div>
    }
</div>
Controller
public ActionResult Index()
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Info("Inizio esecuzione Index()");
            try
            {
                Syncfusion.JavaScript.Models.DialogProperties dia = new Syncfusion.JavaScript.Models.DialogProperties();
                dia.ShowOnInit = false;
                ViewBag.Dia = dia;
                log.Info("Fine esecuzione Index()");
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Errore:{0}", ex.InnerException == null ? ex.Message : ex.InnerException.Message));
                throw ex;
            }
            return View();
        }

In HttpPost action I use this code:

TempData["error"= Resources.Login.CredenzialiErrate;
                                Syncfusion.JavaScript.Models.DialogProperties dia = new Syncfusion.JavaScript.Models.DialogProperties();
                                dia.ShowHeader = true;
                                dia.Title = "Error Message";
                                dia.ContentTemplate = new MvcTemplate<object>
                                {
                                    RazorViewTemplate = (data) =>
                                    {
                                        return "<div> Message </div>"// return the error message in the Dialog content template like this  
                                    }
                                };
                                dia.ShowOnInit = true;
                                ViewBag.Dia = dia;
                                return PartialView("_Login");




AP Arun Palaniyandi Syncfusion Team January 22, 2018 01:39 PM UTC

Hi Pio Luca Valvona, 

Thanks for your update. 

We have tried to reproduce the reported issue by loading the Dialog in the Partial view and hence we can able to reproduce the issue. After validating we found that this issue is because the scripts and themes is not referred to the loaded partial view. Hence in order to overcome this issue you need to refer the layout.cshtml page reference in common. Hence this will load all the necessary scripts and themes to the EJ components in all pages. 

 
_Login.Cshtml  

@{ 
    ViewBag.Title = "DialogPartial"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; // refer the layout page 
} 
 
 
////// your partial page content 


Please find the below sample for reference. 



When the Unobtrusive mode is enabled as true, please find the below KB Link to render our controls https://www.syncfusion.com/kb/7019/how-to-render-our-controls-via-ajax-partialview-in-unobtrusive-true-mode 


Please let us know if you have any concerns. 

Regards,
Arun P. 


Loader.
Up arrow icon