Asp.Net MVC load a partial view using ajax from a JQuery UI tab

前端 未结 1 833
無奈伤痛
無奈伤痛 2021-02-09 22:44

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.

相关标签:
1条回答
  • 2021-02-09 23:12

    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);
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题