2010-01-15 86 views
5

我打開一個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> 
+0

我更新了下面的答案,這應該允許你做你想做的事情,而無需自定義jQuery ajax對象。 – 2010-01-16 20:47:50

回答

2

更新:對不起,我們使用一個定製的包裝。 jQuery默認不包含xmlHttpRequest成功。以下是另一種方法,這種方法在不改變視圖的情況下工作。您基本上只是在響應中檢查id='code'中的元素(如果存在),則顯示錯誤。

success: function(data, textStatus) { 
    if ($("#code",data).length) { //See if the element <whatever id='code'> exists 
    $("#DisplayError").html(data); 
    } else { 
    employeeId = 0; 
    $(this).dialog('close');        
    } 
}, 

這裏是jQuery的1.4版本(見changes here,注意成功回調接收XHR對象作爲第三個參數):

首先,設置的StatusCode 210在您的視圖context.HttpContext.Response.StatusCode = 210;,然後使用這個回調格式:

success: function(data, textStatus, xhr) { 
    if (xhr.status == 210) { 
    $("#DisplayError").html(data); 
    } else { 
    employeeId = 0; 
    $(this).dialog('close');        
    } 
}, 
+0

我收到一個錯誤:「xhr undifined」 – 2010-01-16 16:50:53

+0

然後我仍然必須使用「jqueryShowResult」?它在FF,IE,Chrome上工作嗎? – 2010-01-16 23:41:31

+0

這個解決方案不需要'jQueryShowResult',它應該可以在所有瀏覽器中工作,只需在'ajax結果內嚴格尋找一個帶'id ='code''的元素,這就是'',data'在選擇。 – 2010-01-17 14:38:53