PDA

View Full Version : [CLOSED] Show a modal dialog when exception is caught with message (MVC)



pawangyanwali
Nov 25, 2012, 9:05 PM
We are trying to find a solution to show a modal message when an exception occurs . We tried this but is not working. If we assign it to the directEventUrl in the button it works but does not work if it is not event. How can we achieve this as this is a critial part of our devlopment.

Index.cshtml

@{
ViewBag.Title = "Index";
}
@(Html.X().ResourceManager(ViewBag.ManagerConfig as MvcResourceManagerConfig))
@(Html.X().Panel()
.Items(items => items.Add(new TreePanel
{
ID = "exampleTree",
Header = false,
AutoScroll = true,
Lines = false,
UseArrows = true,
CollapseFirst = false,
RootVisible = false,
Store =
{
new TreeStore
{
Root =
{
new Node
{
NodeID = "Root",
Expanded = true
}
},

Proxy =
{
new AjaxProxy
{
API =
{
Read = Url.Action("GetTreeNodes"),


},


ActionMethods =
{
Read = HttpMethod.GET

},



}
}
}
},
}

))
)


SampleController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
namespace CompDesktop.Controllers
{
public class SampleController : Controller
{
//
// GET: /Sample/

public ActionResult Index()
{
return View();
}

public ActionResult GetTreeNodes()
{
var nc = new NodeCollection(false);
try
{
for (int i = 0; i < 10; i++)
{
Node n = new Node()
{
Text = "Node " + i,
IconCls = "",
Icon = Icon.Application
};

AddChildNode(n);
nc.Add(n);
}
//we are causing the exception for testing
int a = 0;
int b = 1;
int c = b / a;
return this.Content(c.ToString());
}
catch (Exception ex)
{
//this is not working
X.MessageBox.Alert("Error", ex.Message).Show();
return this.Direct();

}
}
private void AddChildNode(Node n)
{
for (int i = 0; i < 5; i++)
{
n.Children.Add(new Node() { Text = "Child " + i, Icon = Icon.ApplicationDouble, Leaf = true });
}
}

}
}

Daniil
Nov 26, 2012, 7:06 AM
Hi Pawan,

Please take a look at this example.

Example View

<!DOCTYPE html>

<html>
<head>
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
@(Html.X().ResourceManager())
@(
Html.X().TreePanel()
.Title("Tree")
.Height(500)
.Width(200)
.Border(false)
.Store(
Html.X().TreeStore()
.Proxy(
Html.X().AjaxProxy()
.Url(Url.Action("GetChildren"))
.Listeners(ls => ls.Exception.Handler = "alert(operation.getError());")
.Reader(Html.X().JsonReader().MessageProperty("message"))
)
.ShowWarningOnFailure(false)
)
.Root(
Html.X().Node().NodeID("0").Text("Root")
)
)
</body>
</html>


Example Controller Action

public StoreResult GetChildren(string node)
{
NodeCollection nodes = new Ext.Net.NodeCollection();

if (!string.IsNullOrEmpty(node))
{
for (int i = 1; i < 6; i++)
{
Node asyncNode = new Node();
asyncNode.Text = node + i;
asyncNode.NodeID = node + i;
nodes.Add(asyncNode);
}

for (int i = 6; i < 11; i++)
{
Node treeNode = new Node();
treeNode.Text = node + i;
treeNode.NodeID = node + i;
treeNode.Leaf = true;
nodes.Add(treeNode);
}
}

//return this.Store(nodes.ToJson); // if success
return this.Store("Some error occured"); //if error
}