Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:30:36+00:00 2026-06-09T00:30:36+00:00

I have a reusable component implemented as an MVC3 PartialView. The component includes jquery-ui

  • 0

I have a reusable component implemented as an MVC3 PartialView. The component includes jquery-ui widgets which need javascript initialization code to run. The following is a simplified example:

==== MySimplifiedPartialView.cshtml ====

<div id="myTabs">
    <ul>
        <li><a href="#myTabs-tab1">Tab 1</a></li>
        <li><a href="#myTabs-tab2">Tab 2</a></li>
        <li><a href="#myTabs-tab3">Tab 3</a></li>
        <li><a href="#myTabs-tab4">Tab 4</a></li>
    </ul>
    <div id="myTabs-tab1">
       ...content
    </div>
    <div id="myTabs-tab2">
       ... content
    </div>
    ... etc ...
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#myTabs").tabs();
    });
</script>

When this PartialView is loaded into a page normally, the initialization code in $(document).ready is run and the widgets are initialized properly (in this case as jquery-ui tabs).

However I also want to be able to load this PartialView into the DOM dynamically, e.g. as the result of an AJAX request. What is the usual way to ensure the initialization code is run in this case? In ASP.NET WebForms apps, I would use an UpdatePanel, and achieve this by handling the load event, e.g.:

Sys.Application.add_load(function () {
    $("#myTabs").tabs();
});

What is the equivalent in an MVC3 applications?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:30:37+00:00Added an answer on June 9, 2026 at 12:30 am

    You have a couple of options. Easiest is often to include the script in the partial view:

    <div id="#partial_view">Here is content in my partial view</div>
    <script type="text/javascript">
        (function($) {
            $('#partial_view #myTabs').tabs();
        } (jQuery));
    </script>
    

    tsegay also shows that you can do it during your ajax success event.

    As a variation on the second option, since you are worried about encapsulation, is to initialize the javascript behaviors unobtrusively. For example:

    <div data-tabs="true">Here is content in my partial view</div>
    

    You can then encapsulate the initialization into a common object like so:

    <script type="text/javascript">
        (function($) {
            $.myApp = {
                obtruders: {}
            };
    
            // apply obtrusive behaviors
            $.myApp.obtrude = function (selector, obtruders) {
                var obtruder;
                obtruders = obtruders || $.myApp.obtruders;
                for (obtruder in obtruders) { // execute every registered obtruder
                    if (obtruders.hasOwnProperty(obtruder)) { // skip any inherited members
    
                        // apply an unobtrusive behavior
                        if (typeof obtruders[obtruder] === 'function') {
                            obtruders[obtruder].apply(this,
                                Array.prototype.slice.call(arguments, 0, 1) || document);
                        }
    
                        // apply all unobtrusive behaviors in set
                        if (typeof obtruders[obtruder] === 'object') {
                            $.myApp.obtrude(selector, obtruders[obtruder]);
                        }
                    }
                }
            };
    
            // apply tabs behavior
            $.extend($.myApp.obtruders, {
                tabs: function (selector) {
                    $(selector).find('[data-tabs=true]').tabs();
                }
            });
    
            $(function() {
                // initialize unobtrusive behaviors
                $.myApp.obtrude(document);
            );
    
        } (jQuery));
    </script>
    

    There is some initial setup in the code above, but ultimately you can do this during your ajax success event:

    success: function(html) {
        $('#partial_placeholder').html(html);
        $.myApp.obtrude($('#partial_placeholder'));
    }
    

    The view doesn’t need to know that it has to call .tabs() on anything in the partial view, just that the partial has some unobtrusive UX behaviors. You can do the same thing with unobtrusive datepickers or other things that need initialized. All you would need is another $.extend section in the $.myApp object:

    // apply datepickers
    $.extend($.myApp.obtruders, {
        datepickers: function(selector) {
            $(selector).find('[data-datepicker=true]').datepicker();
        }
    });
    

    Another advantage of this is that you don’t need to do any initialization on your normal (non-partial) views. The $(function(){..}); at the end of the big script above takes care of that for you by calling $myApp.obtrude(document) when the script first loads into the browser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed some reusable android component which is basically a class . This
I would like to have a reusable ui component which is tied to the
We have quite a bit of reusable code at work (a good thing). Whenever
I have a need for unique reusable ids. The user can choose his own
I am a bit new to reusable plugins for jquery. I have ran across
I need to create a custom, reusable view component. I've been trying to look
I am trying to create a reusable Cache Manager component which can be plugged
I am new to component development. I have designed a file manager using jQuery
I would like to have reusable ratings (typical layout with 5 stars). I have
I have a form which I want to be 'resusable' for a variety of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.