Render Partial View Using jQuery in ASP.NET MVC

后端 未结 8 2042
有刺的猬
有刺的猬 2020-11-22 11:13

How do I render the partial view using jquery?

We can render the partial View like this:

<% Html.RenderPartial(\"UserDetails\"); %>


        
相关标签:
8条回答
  • 2020-11-22 11:27

    I have used ajax load to do this:

    $('#user_content').load('@Url.Action("UserDetails","User")');
    
    0 讨论(0)
  • 2020-11-22 11:36

    I did it like this.

    $(document).ready(function(){
        $("#yourid").click(function(){
            $(this).load('@Url.Action("Details")');
        });
    });
    

    Details Method:

    public IActionResult Details()
            {
    
                return PartialView("Your Partial View");
            }
    
    0 讨论(0)
  • 2020-11-22 11:37

    @tvanfosson rocks with his answer.

    However, I would suggest an improvement within js and a small controller check.

    When we use @Url helper to call an action, we are going to receive a formatted html. It would be better to update the content (.html) not the actual element (.replaceWith).

    More about at: What's the difference between jQuery's replaceWith() and html()?

    $.get( '@Url.Action("details","user", new { id = Model.ID } )', function(data) {
        $('#detailsDiv').html(data);
    }); 
    

    This is specially useful in trees, where the content can be changed several times.

    At the controller we can reuse the action depending on requester:

    public ActionResult Details( int id )
    {
        var model = GetFooModel();
        if (Request.IsAjaxRequest())
        {
            return PartialView( "UserDetails", model );
        }
        return View(model);
    }
    
    0 讨论(0)
  • 2020-11-22 11:40

    You'll need to create an Action on your Controller that returns the rendered result of the "UserDetails" partial view or control. Then just use an Http Get or Post from jQuery to call the Action to get the rendered html to be displayed.

    0 讨论(0)
  • 2020-11-22 11:40

    Using standard Ajax call to achieve same result

            $.ajax({
                url: '@Url.Action("_SearchStudents")?NationalId=' + $('#NationalId').val(),
                type: 'GET',
                error: function (xhr) {
                    alert('Error: ' + xhr.statusText);
    
                },
                success: function (result) {
    
                    $('#divSearchResult').html(result);
                }
            });
    
    
    
    
    public ActionResult _SearchStudents(string NationalId)
            {
    
               //.......
    
                return PartialView("_SearchStudents", model);
            }
    
    0 讨论(0)
  • 2020-11-22 11:43

    You can't render a partial view using only jQuery. You can, however, call a method (action) that will render the partial view for you and add it to the page using jQuery/AJAX. In the below, we have a button click handler that loads the url for the action from a data attribute on the button and fires off a GET request to replace the DIV contained in the partial view with the updated contents.

    $('.js-reload-details').on('click', function(evt) {
        evt.preventDefault();
        evt.stopPropagation();
    
        var $detailDiv = $('#detailsDiv'),
            url = $(this).data('url');
    
        $.get(url, function(data) {
            $detailDiv.replaceWith(data);         
        });
    });
    

    where the user controller has an action named details that does:

    public ActionResult Details( int id )
    {
        var model = ...get user from db using id...
    
        return PartialView( "UserDetails", model );
    }
    

    This is assuming that your partial view is a container with the id detailsDiv so that you just replace the entire thing with the contents of the result of the call.

    Parent View Button

     <button data-url='@Url.Action("details","user", new { id = Model.ID } )'
             class="js-reload-details">Reload</button>
    

    User is controller name and details is action name in @Url.Action(). UserDetails partial view

    <div id="detailsDiv">
        <!-- ...content... -->
    </div>
    
    0 讨论(0)
提交回复
热议问题