views:

4484

answers:

3

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?

+2  A: 

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.

Chris Pietschmann
+17  A: 

You can't. You can, however, call a method (action) that will render the partial view for you.

$.get( '<%= Url.Action("details","user", new { id = Model.ID } %>',
       function(data) {
           $('#detailsDiv').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 Partial( "UserDetails", model );
}
tvanfosson
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.
chobo2
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.
tvanfosson
+13  A: 

I have used ajax load to do this:

$('#user_content').load('/User/UserDetails');
Prasad