Render Partial View Using jQuery in ASP.NET MVC
Asked Answered
G

8

235

How do I render the partial view using jquery?

We can render the partial View like this:

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

How can we do the same using jquery?

Gethsemane answered 15/10, 2009 at 3:22 Comment(3)
You could have a look at below article as well. tugberkugurlu.com/archive/… It follows a different approach and enhances the way.Bathsheba
Stupid question. Is UserDetails a partial view as a cshtml page: UserDetails.cshtml? I am trying to load a partial view . And normally I would use: @Html.Partial("~/Views/PartialViews/FirstPartialViewTwo.cshtml")Seismism
@GeorgeGeschwend, Nothing is stupid here, till someone can respond to it. UserDetails(UserDetails.cshtml) is the Partial View inside the User Controller. As in the comments of the marked answer, its better to use Url.Action instead of hard coding the full path of the view.Gethsemane
G
163

I have used ajax load to do this:

$('#user_content').load('@Url.Action("UserDetails","User")');
Gethsemane answered 12/11, 2009 at 11:37 Comment(12)
Generally I think you're better off going with the Url.Action helper instead of hard-coding the path. This is going to break if your web site is in a subdirectory rather than at the root. Using the helper fixes that problem and allows you to add parameters with dynamically set values.Shelving
You could do $('#user_content').load('@Url.Content("~/User/UserDetails")') to get around that- i often use this method if i need the javascript to slap on the querystring params at the end of the urlKappa
In this answer, UserDetails is a name of an action, not a partial view, right?Reece
Yes. UserDetails is the name of an action which returns a PartialView.Gethsemane
@Gethsemane : Urls should always be evaluated using @Url.Action("ActionName","ControllerName", new { area = "AreaName" } ) instead doing Handcoding.Nahshun
@PKKG. @Url.Action() only evaluates in Razor. this doesn't work if OP wants to put their code in a separate js file and reference it.Jacquijacquie
@Jacquijacquie : can't i initialize the js variable in View and access it in Js File ??Nahshun
@PKKG: Sure you can do that, but that defeats putting all your js code in it's own file.Jacquijacquie
@prasad i need to send model of partial view to action then do some tasks finally update model and send back to partial and update partial. How could i send model for example as form.serialize in load? i think it's impossible, but if you have any idea please share.Anodize
Be careful of this solution. IE has tends to cache the content of the partial view. Since there is no parameter here that disables the cache, you can find yourself loading stale data.Sort
@Gethsemane what is #user-content here?Adolf
@Adolf its the id of a div elementGethsemane
S
303

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>
Shelving answered 15/10, 2009 at 3:26 Comment(22)
I keep getting bad request with this example code your gave. I copied as is and just changed the Controller action it should go to. I am not sure what "user" is for.Blaineblainey
I just used some "likely" controller and action names since you didn't include any code that we could go by. Just replace "details" with your action and "user" with your controller name.Shelving
any idea how this would work with Razor? tried $.get( "@Url.Action(\"Manifest\",\"Upload\", new { id = " + key + " })", function(data) { $("<div/>").replaceWith(data); } );Ho
@Patrick - I'd use single quotes (double is harder to read) for the JS. Regardless you don't need to "quote" the quote characters or construct a string -- just let the UrlHelper do it's work. See my updated example.Shelving
Works awesome thanks! Why is it caching the Partial View? In my partial view I'm simply outputting the current date and time.Affinity
My bad, the issue was that the element was replaced and was no longer available for replacement the second time.Affinity
Superb solution. And fixed a treeview state issue for meCypripedium
Looking at the supplied Controller code - Any chance the 'Partial' is supposed to be 'PartialView'? I'm getting an error as described here: #17206388Prentiss
@Prentiss nice catch! Updated.Shelving
@tvanfosson, please consider the usage of .html. I was forced to create some trick $('#partialView').children('span').replaceWith('<span>' + data + '</span>'); to always have a span to replace with the new content. Best LuisCletuscleve
@Custódio it's just an example, but the idea would be that the partial itself would be an entire container, i.e., be wrapped with <div id="detailsDiv">...</div>, then you would simply replace the entire container with the new view. If your partial doesn't include the container then just use $('#detailsDiv').html(data);Shelving
How to access "model" from within javascript ?Aggri
Very good stuff. I wonder if the first poster's idea has any merit? It'd be nice to address the replaceWith challenge for completeness.Mcgaha
Is there a way to avoid inline javascript using this solution?Ricks
@Ricks - there's no requirement for inline JavaScript with this. All of the JavaScript can be in separate files.Shelving
@Shelving So javascript supports razor?Ricks
@Ricks I see what you mean, but really that's only for this particular example. It's hard to show how you'd hook it together otherwise. In reality I'd probably use data attributes for the glue.Shelving
I use this method to load a table. But now for some reason my Jquery data-table is not working? And I think it has to do with loading the content in.Foramen
@Foramen - if you're reloading the entire table, you might need to reapply the plugin since the DOM elements it was originally connected to have been replaced. It might be better to connect it to a method that returns the data as JSON, datatables.net/examples/data_sources/server_side.htmlShelving
@Shelving I tried this example with a cshtml page. Well it didn't work. I a guessing I need to try another approach(?). I left the div empty thinking it would load the partial view into the <div id="detailsDiv">. I debugged through the jquery and the Action method and that part works. but after I return: return PartialView("UserDetails", model); in the Details action method the div is empty. What am I doing wrong? this might be the wrong approach(?). Do you or anyone have any ideas?Seismism
@GeorgeGeschwend I'm confident that this method works. Check to see if you're seeing errors in the console. Check if the jQuery selectors find the elements you're expecting (with the browser debugger). Make sure that the partial view you're returning via AJAX really contains the data you expect.Shelving
@Shelving So for what I was doing, it worked (!) finally. It boiled down to a spelling error. eye roll $detailDiv is what I had created as a var in jquery, ergo: var $detailDiv = $('#detailsDiv'),... and later in the jquery I had $detailsDiv...ergo: $detailsDiv.replaceWith(data); so that screwed me up royally. Thanks its a great example! :)Seismism
G
163

I have used ajax load to do this:

$('#user_content').load('@Url.Action("UserDetails","User")');
Gethsemane answered 12/11, 2009 at 11:37 Comment(12)
Generally I think you're better off going with the Url.Action helper instead of hard-coding the path. This is going to break if your web site is in a subdirectory rather than at the root. Using the helper fixes that problem and allows you to add parameters with dynamically set values.Shelving
You could do $('#user_content').load('@Url.Content("~/User/UserDetails")') to get around that- i often use this method if i need the javascript to slap on the querystring params at the end of the urlKappa
In this answer, UserDetails is a name of an action, not a partial view, right?Reece
Yes. UserDetails is the name of an action which returns a PartialView.Gethsemane
@Gethsemane : Urls should always be evaluated using @Url.Action("ActionName","ControllerName", new { area = "AreaName" } ) instead doing Handcoding.Nahshun
@PKKG. @Url.Action() only evaluates in Razor. this doesn't work if OP wants to put their code in a separate js file and reference it.Jacquijacquie
@Jacquijacquie : can't i initialize the js variable in View and access it in Js File ??Nahshun
@PKKG: Sure you can do that, but that defeats putting all your js code in it's own file.Jacquijacquie
@prasad i need to send model of partial view to action then do some tasks finally update model and send back to partial and update partial. How could i send model for example as form.serialize in load? i think it's impossible, but if you have any idea please share.Anodize
Be careful of this solution. IE has tends to cache the content of the partial view. Since there is no parameter here that disables the cache, you can find yourself loading stale data.Sort
@Gethsemane what is #user-content here?Adolf
@Adolf its the id of a div elementGethsemane
C
63

@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);
}
Cletuscleve answered 21/6, 2013 at 15:52 Comment(0)
C
12

Another thing you can try (based on tvanfosson's answer) is this:

<div class="renderaction fade-in" 
    data-actionurl="@Url.Action("details","user", new { id = Model.ID } )"></div>

And then in the scripts section of your page:

<script type="text/javascript">
    $(function () {
        $(".renderaction").each(function (i, n) {
            var $n = $(n),
                url = $n.attr('data-actionurl'),
                $this = $(this);

            $.get(url, function (data) {
                $this.html(data);
            });
        });
    });

</script>

This renders your @Html.RenderAction using ajax.

And to make it all fansy sjmansy you can add a fade-in effect using this css:

/* make keyframes that tell the start state and the end state of our object */
@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

.fade-in {
    opacity: 0; /* make things invisible upon start */
    -webkit-animation: fadeIn ease-in 1; /* call our keyframe named fadeIn, use animattion ease-in and repeat it only 1 time */
    -moz-animation: fadeIn ease-in 1;
    -o-animation: fadeIn ease-in 1;
    animation: fadeIn ease-in 1;
    -webkit-animation-fill-mode: forwards; /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
    -o-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
    -webkit-animation-duration: 1s;
    -moz-animation-duration: 1s;
    -o-animation-duration: 1s;
    animation-duration: 1s;
}

Man I love mvc :-)

Convey answered 29/10, 2014 at 14:58 Comment(2)
Why did you each function? How it works? Do u mena something like: data-actionurl="@Url.Action("details","user", new { id = Model.ID } data-actionurl="Another Action"?Thaddeusthaddus
No, the each function loops over all html elements that have the data-actionurl attribute and fills it by invoking an ajax request for the action method. So multiple <div class="renderaction fade-in" ...></div> elements.Convey
C
10

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.

Congratulation answered 15/10, 2009 at 3:25 Comment(1)
how to set time interval to refresh updated data in this jQuery functionWalz
P
6

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);
        }
Prepossess answered 15/12, 2018 at 17:4 Comment(0)
F
1

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");
        }
Fabrikoid answered 19/9, 2017 at 5:37 Comment(0)
S
1

If you need to reference a dynamically generated value you can also append query string paramters after the @URL.Action like so:

    var id = $(this).attr('id');
    var value = $(this).attr('value');
    $('#user_content').load('@Url.Action("UserDetails","User")?Param1=' + id + "&Param2=" + value);


    public ActionResult Details( int id, string value )
    {
        var model = GetFooModel();
        if (Request.IsAjaxRequest())
        {
            return PartialView( "UserDetails", model );
        }
        return View(model);
    }
Sedgewick answered 25/7, 2019 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.