1

I am creating an ajax.actionlink like below bor my bootstap pop up but insted of opening it in a pop up it redirect to an url saying controller/action

My code is as below

   @Ajax.ActionLink("CheckOut", "CheckOut", "Home", null, new AjaxOptions
            {
                HttpMethod = "Post",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "myModal"
            }, new
            {
                @class = "btn btn-primary btn-lg",
                @id = "chkOut"
            })

  $(document).ready(function () {
        $("#chkOut").attr('data-toggle', 'modal')
    });
1
  • That link is posting your actual page, by default after a post mvc goes for your controller and redirects, one solution should be to create a jquery click event in that link and use e.preventDefault() Commented Apr 16, 2014 at 13:29

2 Answers 2

2

Use OnComplete property of Ajaxoptions like this:

 @Ajax.ActionLink("CheckOut", "CheckOut", "Home", null, new AjaxOptions
            {
                HttpMethod = "Post",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "myModal",
                OnComplete = "ShowPopup",
            }, new
            {
                @class = "btn btn-primary btn-lg",
                @id = "chkOut"
            })

Here is function which will be called on Complete of ajax call of action link:

function ShowPopup()
{
    $("#chkOut").attr('data-toggle', 'modal');
}
Sign up to request clarification or add additional context in comments.

Comments

1
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" 
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"></h4>

            </div>
            <div class="modal-body"><div class="te">Please wait...</div></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>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>


{<script>
    $(document).ready(function () {         


        $("body").on("click", "a.dialog-window", null, function (e)
        {

            e.preventDefault();


            var $link = $(this); // Reference to <a/> link
            var title = $link.text();// this is title to fetch in htnl    
            $('#myModal .modal-title').html(title);

            var url = $(this).attr('href');
            if (url.indexOf('#') == 0) {
                $('#myModal').modal('show');
            }
            else
            {

                $.get(url, function (data) {
                    $('#myModal .te').html(data);
                    $('#myModal').modal();
                }).success(function () { $('input:text:visible:first').focus(); });

            }

        });
    });

</script>}


{
  @Html.ActionLink("Profile II", "Create", "PatientProfile", new { id = item.PatientId }, new { @class = "dialog-window" }) 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.