1

I have a ASP MVC application and I have a main view that will load a Modal and inside the modal I load a Partial View.

This is my modal div (Bootstrap)

<!-- Modal -->
<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-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">Cerrar</button>
                <button type="button" class="btn btn-primary">Generar</button>
            </div>
        </div>
    </div>
</div>

And using jQuery I show my modal:

$('#btnFilter').click(function() {

                var url = '@Url.Action("AdvanceViewPartial", "Stats")';

                $('#myModal .modal-title').html('Filters');
                $('#myModal .modal-body').load(url);
                $('#myModal').modal('show');
            });

My Partial View "AdvanceViewPartial" has javascript code also inside that is not being fired when the modal shows my partial view.

Here is my partial View code:

<div class="widget">

    <form id="frmPass" action="" role="form">

<div class="form-group">
                    <label>Start Date</label>
                    <input class="form-control" id="dtstartdate" placeholder="dd/mm/aaaa">
                </div>


    </form>
</div>


@section script {

    <script type="text/javascript">


        $(document).ready(function() {

            $("#dtstartdate").datepicker({
                showButtonPanel: true
            });


        });

    </script>

}

Any clue on why on my Partial View when show as modal my jQuery code is not working. I can't get my date field to be datepicker.

VAAA
  • 14,531
  • 28
  • 130
  • 253
  • 1
    Remove the script from your partial (scripts should never be in partials - only the layout or main view). In the main view's script, attach the plugin after you have added the modal to the DOM –  Apr 22 '15 at 22:59
  • What @StephenMuecke comments, in a partial view should never be in partials, and the `@section script {}` wil not work in partialviews. Here is an answer http://stackoverflow.com/questions/7556400/injecting-content-into-specific-sections-from-a-partial-view-asp-net-mvc-3-with – Jorge F Apr 22 '15 at 23:09

0 Answers0