2016-08-29 5 views
1

부트 스트랩 모달로 두 가지 작업을하고 있습니다. 첫 번째는 MVC 부분 뷰에서 다이나믹하게로드하는 것입니다. 그 부분이 효과가있는 것 같습니다. 이제 저는 Modal을 페이지 중앙에 놓으려고합니다.일부 자바 스크립트로 부트 스트랩 모달을 가운데로 가져올 수 없습니다.

이 예제를 사용하고 있지만 작동하지 않는 것 같습니다. https://www.abeautifulsite.net/vertically-centering-bootstrap-modals

JavaScript의 대화 ID 이름을 ".modal-container"로 변경하여 모달을 찾아야합니다. 그 외에는 내가 바꿀 필요가있는 다른 것이 보이지 않습니다. 필자는 F12 Dev Tools를 살펴 보았지만 오류는 보이지 않습니다.

Index.cshtml

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
    <link href="~/Content/Site.css" rel="stylesheet" /> 
</head> 
<body> 
    <div> 
     <div class="container"> 
      @Html.ActionLink("Test Modal ", "TestModal", "Test", null, new { @class = "modal-link btn btn-warning" }) 
     </div> 
    </div> 

    <div id="modal-container" class="modal fade" role="dialog"> 
     <div class="modal-content"> 
     </div> 
    </div> 

    <script src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script src="~/Scripts/bootstrap.min.js"></script> 
    <script src="~/Scripts/modernizr-2.6.2.js"></script> 

    <script type="text/javascript"> 

     //**** Bootstrap Modal - used with Partial Views *********************** 
     $(function() { 
      // Attach modal-container bootstrap attributes to links with .modal-link class. 
      // when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog. 
      $('body').on('click', '.modal-link', function (e) { 
       e.preventDefault(); 
       $(this).attr('data-target', '#modal-container'); 
       $(this).attr('data-toggle', 'modal'); 
      }); 

      // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears 
      $('body').on('click', '.modal-close-btn', function() { 
       $('#modal-container').modal('hide'); 
      }); 

      //clear modal cache, so that new content can be loaded 
      $('#modal-container').on('hidden.bs.modal', function() { 
       $(this).removeData('bs.modal'); 
      }); 

      $('#CancelModal').on('click', function() { 
       return false; 
      }); 
     }); 

     /** 
     * Vertically center Bootstrap 3 modals so they aren't always stuck at the top 
     */ 
     $(function() { 

      function reposition() { 

       var modal = $(this), 
        dialog = modal.find('.modal-container'); 

       modal.css('display', 'block'); 

       // Dividing by two centers the modal exactly, but dividing by three 
       // or four works better for larger screens. 
       dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height())/2)); 

      } 

      // Reposition when a modal is shown 
      $('.modal').on('show.bs.modal', reposition); 

      // Reposition when the window is resized 
      $(window).on('resize', function() { 
       $('.modal:visible').each(reposition); 
      }); 

     }); 
    </script> 

</body> 
</html> 

컨트롤러

public class TestController : Controller 
    { 
     // GET: Test 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     public ActionResult TestModal() 
     { 
      return PartialView("_TestModal"); 
     } 
    } 

_TestModal.cshtml

<div class="modal-body"> 
    <h4> 
     Are you sure you want to delete this cost center from all clocks in the group? 
    </h4> 

    @using (Html.BeginForm("Lyubomir", "Home", FormMethod.Post)) 
    { 
     <div class="row"> 
      &nbsp; 
     </div> 
     <div class="row"> 
      <div class="col-md-4 col-md-offset-4"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
       <button type="submit" id="approve-btn" class="btn btn-danger">Save</button> 
      </div> 
     </div> 
    } 
</div> 


<script type="text/javascript"> 
    $(function() { 
     $('#approve-btn').click(function() { 
      $('#modal-container').modal('hide'); 
     }); 
    }); 
</script> 

편집 : 내가 모달 그냥 상단에 표시되는 초기 게시물에 언급해야 페이지의 너비가 100 %가됩니다.

dialog = modal.find('.modal-container'); 

이 modal- '의 클래스와 모든 요소를 ​​찾습니다

enter image description here

+0

센터? 아니면 수평으로? (자동으로 수평 중심에 놓여 야합니다 ...) 그리고 수직적이면 브라우저 뷰포트에서 의미합니까? –

+0

죄송합니다. 분명히해야합니다. 내 게시물을 업데이트했습니다. – Caverman

답변

2

글쎄, 난 그냥보고 코드에서 매우 간단한 실수 (내가 생각하는 것을) 발견 컨테이너'.

그러나 코드 조각에는 CLASS가 아닌 'modal-container'로 설정된 div의 ID가 있습니다.

위의 코드 줄은 원하는 div를 찾지 못합니다. 사실, 나는 어떤 요소라도 찾고 있다고 생각하지 않는다. ('모달 컨테이너'의 클래스가 없기 때문에).

dialog = modal.find('#modal-container'); 

알려주세요 : 같은 말을 할 수 modal.find()를

<div class="modal fade modal-container" role="dialog"> 
    <div class="modal-content"> 
    </div> 
</div> 

또는는 HTML에게 동일을 남겨 변경 :

그래서 어쩌면 같은으로 변경 도움이된다면.


편집 : 귀하의 부트 스트랩 모달 구조가 꺼져 있기 때문에

귀하의 모달 전체 폭이다. div가 누락되었습니다.

은 당신이 무엇을 :

<div id="modal-container" class="modal fade" role="dialog"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
    </div> 
    </div> 

'모달의 클래스와 사업부를 추가 :

여기
<div id="modal-container" class="modal fade" role="dialog"> 
    <div class="modal-content"> 
    </div> 
</div> 

는 당신이 필요로하는 무엇을 (당신의 문제를 해결해야한다)입니다 -dialog '가 도움이됩니다.

아, 그리고에 modal.find() 변경 : 수직 ....

dialog = modal.find('.modal-dialog'); 
+0

나는 그것이 문제가 될 것이라고 확신했고 그것은 분명히 문제의 일부입니다. modal.find ('# modal-container')로 변경하고 DIV의 ID를 동일하게 둡니다. 불행하게도 나는 어떤 변화도 보지 못했다. dev 도구에는 오류가 없습니다. 브라우저를 닫았다가 다시 열었을 때 임시 인터넷 파일을 안전한쪽으로 만 지웠습니다. – Caverman

+0

모달을 덮어 쓰는 것이 있는지 궁금합니다. 나의 이해에서 모달의 폭은 100 %가되어서는 안된다. 기본 동작은 모달을 브라우저 상단에 배치하는 것이지만 100 % 너비가 아니라고 생각합니다. – Caverman

+0

방금 ​​왜 그런 문제가 있는지 깨달았습니다. 당신은 또한 부트 스트랩 모달을 위해 존재하는 전체 div 엘리먼트를 놓치고있다. 게시물 (하단)에서 나의 최신 편집을 확인하고 나에게 돌아 가라. –

관련 문제