2013-04-11 2 views
1

PartialView 또는 다른 뷰를 액션 메소드에서 Ajax 포스트로 되돌려 놓으려고합니다. 나는 Ajax 성공 함수로부터 Jquery Modal 팝업으로 내용 ParitalView를 표시하려고했다.JsonResult ActionMethod에서 PartialView를 Ajax 포스트로 돌아가서 해당 PartialView를 모달 팝업으로 표시합니다.

등록 양식이있는 'MyRegistrationView'는 양식 제출 버튼에서 ajax 게시 통화를 언급했습니다.

$.ajax({ 
      url: url,   //http://localhost/MyRegistration/RegisterUser 
      type: 'POST', 
      dataType: 'json', 
      data: ko.toJSON(RegistrationInfoModel), 
      contentType: "application/json; charset=utf-8", 
      success: function (result) { 
       //Do something 
      }, 
      error: function (request, status, error) { 
       //Do something 
      } 
     }); 

위의 ajax 호출은 다음과 같은 작업 방법을 사용하여 "MyRegistrationController"라는 내 컨트롤러로 이동합니다.

[HttpPost] 
public JsonResult RegisterUser(RegistrationInfo model) 
{ 
    //Register User 
    .... 
    if(successful) 
    { 
    return Json(new { data = PartialView("_ShowSuccessfulModalPartial") }); 
    } 

} 

지금

  1. 어떻게 다시 아약스의 '성공'기능에서 '_ShowSuccessfulModalPartial'의 '내용'을 얻을 보여줄 수까지 그 같은 등록 페이지에서 모달 팝있다.
  2. 내가 돌아가려면/다른보기로 리다이렉트하고 싶습니다. 어떻게 할 수 있습니까? JsonResult가 ActionMethod의 반환 유형으로 사용됩니다.
  3. ModalErrors를 등록 과정에서 다시 으로 보내서 ValidationSummary 아래에 표시 할 수 있습니다.

:

답변

2

당신은 JSON 대신 부분 뷰를 반환 할 수 있습니다 (참고 내가 반환 형식으로 JsonResult를 사용하지 않는 경우 내가 예기치 않은 토큰 < 'parseerror'아약스 수).

<div id="dialog-confirm" title="Your title">  
    <div id="dialog-content"></div> 
</div> 

당신이 대화를 초기화해야합니다 : 기본보기에서

은이 같은 대화 HTML (당신이 jQueryUI를 사용하는 assumming)를 추가 shoudl. 당신이 부분 뷰를 반환해야 할 수도 컨트롤러에서

$(document).ready(function() { 
    $("#dialog-confirm").dialog(); 
}); 

:

[HttpPost] 
public virtual ActionResult RegisterUser(RegistrationInfo model) 
{ 
    var model = //Method to get a ViewModel for the partial view in case it's needed. 
    return PartialView("PartialViewName", model); 
} 

당신이 당신의 아약스 요청을 수행 할 때 그런 다음 대화 상자에 부분 뷰를 추가하고 다음을 보여줍니다.

$.ajax({ 
      url: url,   
      type: 'POST', 
      dataType: 'json', 
      data: ko.toJSON(RegistrationInfoModel), 
      contentType: "application/json; charset=utf-8", 
      success: function (result) { 
       $("#dialog-content").empty(); 
       $("#dialog-content").html(result); 
       $("#dialog-confirm").dialog("open"); 
      }, 
      error: function (request, status, error) { 
       //Do something 
      } 
     }); 

희망이 있습니다.

+1

"ActionResult"대신 "JsonResult"를 사용하고 있습니다. 반환 유형으로 JsonResult를 사용하지 않으면 ajax 'parseerror'Unexpected token <이 발생합니다. – user2232861

+0

jsonresult를 반환하는 코드를 사용해 볼 수 있습니다. 이것이 도움이된다면 대답으로 표시하십시오. – lopezbertoni

+0

감사합니다. – user2232861

관련 문제