Hello,
I open a jQuery dialog, in this box I do a save/cancel. To Save, I call my controller, make some validation, save or throw Exception (MyPersonalException). If there is exception, I return an another View (the "MessageError" view) to display in the popup. I just want to see in the modal box the message available in "MyPersonalException"
My questions : 1. That's work but only with Firefox not IE not Chrome 2. Is there an other way because that's look a lof of code to just diplay a message.
The controller look like this :
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveOrUpdate(Guid id, string firstName, string LastName)
{
try
{
Employee employee = new Employee() { Id = id, FirstName = firstName, LastName = LastName };
_employeeService.SaveOrUpdate(employee);
return Index();
}
catch (MyPersonalException ex)
{
_model.ErrorMessage = ex.Message;
return View("MessageError", _model);
}
catch (Exception ex)
{
_model.ErrorMessage = ex.Message;
return View("MessageError", _model);
}
}
To call the dialog box, I use this code
jQuery(document).ready(function() { $(function() { /* var name = $("#firstName"), email = $("#lastName"), password = $("#isActive"), allFields = $([]).add(name).add(email).add(password), tips = $("#validateTips");*/
$("#dialog").dialog({
bgiframe: true,
autoOpen: false,
modal: true,
buttons: {
Save: function() {
$.ajax({
type: "POST",
url: "/Employee/SaveOrUpdate",
data: {
id: getId(),
firstName: getFirstName(),
lastName: getLastName()
},
success: function(data) {
if (jqueryShowResult(data))
$("#DisplayError").html(data);
else {
employeeId = 0;
$(this).dialog('close');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
})
},
Cancel: function() {
employeeId = 0;
$(this).dialog('close');
}
},
close: function() {
$("#gridEmpoyee").trigger("reloadGrid");
},
open: function() {
$.ajax({
type: "POST",
url: "/Employee/GetEmployee",
data: {
id: employeeId
},
success: function(data) {
$("#employeeDetail").html(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
})
}
});
});
});
The jQueryShowResult
<script type="text/javascript">
jqueryShowResult = function(msg) {
var browser;
try //Internet Explorer
{
xmlDocTest = new ActiveXObject("Microsoft.XMLDOM");
browser = "IE";
}
catch (e) {
browser = "FF";
}
if (browser == "IE") {
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(msg);
var message = xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue;
var code = xmlDoc.getElementsByTagName("code")[0].childNodes[0].nodeValue;
return false;
}
catch (e) {
return true;
}
}
else {
var code = $(msg).find('code').text();
var message = $(msg).find('message').text();
if (code == "500") {
return false;
}
else {
return true;
}
}
};
</script>