2010-01-15 3 views
5

jQuery 대화 상자를 엽니 다.이 상자에는 저장/취소가 있습니다. 저장하려면 컨트롤러를 호출하거나 유효성을 검사하거나 Exception (MyPersonalException)을 저장하거나 throw합니다. 예외가 있으면 팝업에 표시 할 다른보기 ("MessageError"보기)를 반환합니다. 그의 LOF 모양입니다 때문에 다른 방법이 있나요 하지만 파이어 폭스와 작업입니다 1.하지 IE는하지 2. 크롬 : 난 그냥 모달 상자에서 "MyPersonalException"UI에 대한 예외 메시지

내 질문에서 사용할 수있는 메시지를보고 싶어 코드를 표시하기 만하면됩니다. 이 같은

컨트롤러의 모양을

[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를 (문서) .ready (함수() { $ (함수() { 를 사용/* var name = $ ("# firstName"), email = $ ("# lastName"), password = $ ("# isActive"), allFields = $ ([]). add (이메일) .add (비밀번호), 팁 = $ ("# 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 객체를 사용자 정의하지 않고도 원하는대로 할 수 있습니다. –

답변

2

업데이트 : 죄송합니다, 우리는 사용자 정의 래퍼를 사용합니다. 기본적으로 jQuery에는 xmlHttpRequest가 포함되지 않습니다. 아래는 다른 접근법입니다.이 방법은보기를 전혀 변경하지 않고 작동합니다. 기본적으로 응답에서 id='code'으로 요소를 확인하는 중입니다. 존재하는 경우 오류를 표시합니다. 첫째,이 콜백을 사용하여 다음보기 context.HttpContext.Response.StatusCode = 210; (210)에 대한 상태 코드를 설정

:

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 객체를 수신) 형식 :

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

오류가 발생했습니다 : "xhr undifined" –

+0

그렇다면 "jqueryShowResult"를 사용해야합니다. FF, IE, Chrome에서 작동합니까? –

+0

이 솔루션과 함께'jQueryShowResult'가 필요없고, 모든 브라우저에서 작동해야합니다. 아약스 결과 내에서'id = 'code' '를 가진 요소를 찾고있을뿐입니다. 즉,'data'가 선택자. –

관련 문제