Display Bootstrap Modal if the condition is true?

c# asp.net .net asp.net-mvc bootstrap-modal

2544 просмотра

2 ответа

45 Репутация автора

I want to display the bootstrap modal on button click in ASp.net MVC if the condition is true or false. Say eg if I check the login credentials and if it fails then how to display the error in a bootstrap modal or bootstrap alerts.

    <html>
<head>
    <link href="~/bootstrap.min.css" rel="stylesheet" />
    <script src="~/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>

     <script>
         $(function()
         {
             $("#mybutton").click(function(){
                 var test1 = $('#test').val();
                 if(test1 == "Value")
                     $("#myModal").modal('show');
             });
         });
     </script>

</head>
<body>

        <input type="text" class="form-control" id="test" placeholder="Enter A Value" />
        <button type="submit" value="Click" name="click" id="mybutton">Click Me!</button>            


    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

How it can be achieved???

Автор: Mayank Bhuvnesh Источник Размещён: 18.07.2016 09:35

Ответы (2)


0 плюса

424 Репутация автора

You can try opening the popup from script.

<script type="text/javascript">
    function OpenPopup() {
        if(condition){
        $("#YourModalId").modal('show');
       }
    }
</script>
Автор: A Bittersweet Life Размещён: 18.07.2016 09:40

2 плюса

141 Репутация автора

Try below code its working fine . i am created link in jsfiddle
http://jsfiddle.net/h3WDq/1543/

  <script>
$(function()
{
  $("#mybutton").click(function(){
  var mycodition=true;
  if(mycodition)
   $("#myModal").modal('show');
     return false;
 });
});

Автор: Surajghosi Размещён: 18.07.2016 09:53
Вопросы из категории :
32x32