我打開一個jQuery對話框,在這個框中我做了一個保存/取消。爲了保存,我調用我的控制器,進行一些驗證,保存或拋出Exception(MyPersonalException)。如果有異常,我會返回另一個View(「MessageError」視圖)以顯示在彈出窗口中。我只是想在模式對話框看到「MyPersonalException」異常消息到UI
我的問題提供的消息: 1.這是工作,但僅與Firefox不IE除了Chrome 2.是否有其他的方式,因爲那是看的LOF代碼只需要顯示一條消息。
控制器是這樣的:
[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);
}
}
要調用的對話框中,我用這個代碼
jQuery的(文件)。就緒(函數(){$ (函數(){ /* 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) {
}
})
}
});
});
});
的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>
我更新了下面的答案,這應該允許你做你想做的事情,而無需自定義jQuery ajax對象。 – 2010-01-16 20:47:50