6

I have several action methods on my controller that return a partial view (ascx), and I want these partial views to be rendered when clicking on the different JQuery UI tabs.

The tabs are defined like this:

<div id="tabs">
  <li><a href="#FirstTab" title="First tab">First tab</a></li>
  <li><%= Html.ActionLink("General", "General", new {id=Model.Id}) %></li>
  <li><%= Html.ActionLink("Details", "Details", new {id=Model.Id}) %></li>
  <div id="FirstTab">
  ...
  </div>
</div>

This renders the tabs fine, the first tab (with static content) is displayed correctly, but when I click the other tabs, nothing happens.

If instead of returning a partial view from the action methods I just return plain content, it renders the content just fine and the tabs work perfectly.

Any idea of what am I doing wrong?

Thanks

3
  • 1
    So far everything looks good. Can you check whether the ajax call is actually occurring? And if it is occurring, what is being returned from the action? You can check this easily with tools like FireBug. Commented Oct 29, 2009 at 15:10
  • Hi Johnny, yes, the call is occurring (I put breakpoints in the actions and they were hit). I fixed the problem using aspx pages instead of ascx (the aspx pages don't have the html, head or body tags, just the control I want to render). This seems strange though, or maybe it's that I don't have clear the difference between View and PartialView Commented Oct 29, 2009 at 16:48
  • I'm glad you were able to find a working solution. Commented Oct 30, 2009 at 3:58

1 Answer 1

11

Hey, I've done the same thing recently. I simplified in order to be more clear:

The html:

    <div class="tabs">
              <ul>
                   <li>
                       <a onclick="TabItemClicked(this,<%=Url.Action("General", new {id=Model.Id}) %>))" href="#fragment1">
                       <span>General</span>
                       </a>
                   </li>
                   <li>
                       <a onclick="TabItemClicked(this,<%= Html.ActionLink("Details", new {id=Model.Id}) %>))" href="#fragment2">
                       <span>Details</span>
                       </a>
                   </li>
              </ul>
              <div id="fragment1"></div>
              <div id="fragment2"></div>
  </div>

and the JQuery code:

function TabItemClicked(a, action) {

    var container = $(a).parents('div.tabs');
    var resultDiv = $($(a).attr('href'), container);

    $.ajax({
        type: "POST",
        url: action,
        data: {},
        success: function(response) {
            resultDiv.html('');
            resultDiv.html(response);
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I know this will work, but I was trying to achieve it with the default functionality of JQuery UI tabs...

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.