Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load ASP.NET MVC partial view on JQueryUI tab selection

I've been searching for the last day and can't find anything on this specific topic asked within the last year or two, and as JQuery appears to deprecate things quite a lot, it seems fair to ask this question with regard to the current JQuery API.

Bit new to JQuery, I'd like to know this;

If I have a tab for every member of a team, and for each of those tabs a table of data is loaded in a partial view from a database, when I load the initial page will all the tabs be populated with their partials or will they only be populated when the tab is clicked?

If the tabs are populated on page load, is there a way to make it so that the tabs only populate when clicked? I can provide the source code if there's any point in doing so, just comment and ask for it.

EDIT 1

As I'm using ASP.NET MVC to render the partial views with contextual information, is it literally just the "ajax/" that is important in the href or does the href need to link to static content? Question regards:

<div class="h-single" id="users">
    <ul>
        @{
            foreach (var user in Model)
            {
                <li><a href="ajax/"[email protected]><span>@user.Name</span></a></li>
            }
        }
    </ul>
    @{
        foreach (var user in Model) 
        {
            <div [email protected]>@Html.Action("_userAdvisoryRequests", new { username = user.EHLogin })
            </div>
        }
    }
</div>

Just noticed you don't need the divs for ajax content so that ain't gonna work either.

EDIT 2

Solution:

<div class="h-single" id="users">
    <ul>
        @{
            foreach (var user in Model)
            {
                <li><a [email protected]("_partial","Home", new { param = @user.Param })><span>@user.Name</span></a></li>
            }
        }
    </ul>
</div>

Credits to Ashwini Verma for the answer!

"If the href in the <li><a> tag references a div then the div must be page-loaded in order to be viewed, but if the href references an action or a link then it can be loaded on demand."

like image 556
Inspector Squirrel Avatar asked Aug 01 '14 09:08

Inspector Squirrel


People also ask

How can I load partial view inside the view?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

How do you call a partial view from a different model?

Step 1: Create Models for both Personnel and Professional details. Step 2: Create a regular class file which will act like a DAL, which will supply some data. Step 3: Now Define the Partial Views both for Personnel and Professional details.

How do you call a partial view in MVC Razor?

For this go to Solution Explorer then select Views -> Shared Folder -> Right-click -> Add View. Now for the View -> Home -> Index.


1 Answers

Example given on jQuery site.

Description

If the content pane for a tab references a HTML element then the tab will be page-loaded;

<li><a href="#tabs-1">Preloaded</a></li>

If the content pane for a tab references a URL, the tab will be loaded via AJAX when the tab is selected;

<li><a href="ajax/content1.html">Tab 1</a></li>

Example

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Preloaded</a></li>
    <li><a href="ajax/content1.html">Tab 1</a></li>
    <li><a href="ajax/content2.html">Tab 2</a></li>
    <li><a href="ajax/content3-slow.php">Tab 3 (slow)</a></li>
    <li><a href="ajax/content4-broken.php">Tab 4 (broken)</a></li>
  </ul>
  <div id="tabs-1"></div>
</div>

<script>
  $(function() {
    $( "#tabs" ).tabs({
      beforeLoad: function( event, ui ) {
        ui.jqXHR.error(function() {
          ui.panel.html(
            "Couldn't load this tab. We'll try to fix this as soon as possible. " +
            "If this wouldn't be a demo." );
        });
      }
    });
  });
</script>

Updated

href should be the URL of the action method which loads the partial view. Example

<a href="@Url.Action("YourActionForContent", "Home")">Load Tab Content</a>

Action Method

public PartialViewResult YourActionForContent()
{
    return PartialView("_Content");
}
like image 80
Ashwini Verma Avatar answered Sep 22 '22 13:09

Ashwini Verma