0

I'm just curious and at the same time confused if RenderPartial in MVC(MVC5) is the same as an ajax call using jQuery, or to put simply is RenderPartial Async?

Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54
Reyn
  • 757
  • 1
  • 5
  • 16
  • `if RenderPartial in MVC(MVC5) is the same as an Ajax call using jquery` No. AJAX call is something we do from client side to server. RenderPartial will be executed on server before even page is rendered. – ramiramilu Jul 01 '15 at 09:19
  • RenderPartial and ajax call are not same..RenderPartial render a partial view to view before sending it to client.. while ajax allow pages from client to be updated asynchronously as many time as call.. – Abbas Galiyakotwala Jul 01 '15 at 09:25

1 Answers1

1

No. RenderPartial is sync.

But you can use an ajax call to a sync/async action if you are trying to get a sync, async behaviour.

'if RenderPartial in MVC(MVC5) is the same as an Ajax call using jQuery'

^ Let me explain.

For example you are using RenderPartial to render comments area of a post. From time to time, 30 seconds let's say, you can use an ajax call which will render latest comments and bring back to client-side the rendered html. Then you will replace your comments area html with what you receive from server in the response (ajax success callback).

Par example:

@model WhenToUseRenderActionAndRenderPartial.Models.ShowPostViewModel
@{
    ViewBag.Title = Model.Post.Title;
}

<section>
    <article>
        <header>
            <h1>@Model.Post.Title</h1>       
        Posted on | <time datetime="@Model.Post.DatePublished.ToString("s")">
           @Model.Post.DatePublished.ToLongDateString()
        </time> | @Model.Comments.Count() Comments
        </header>
        @Html.Raw(Model.Post.Content)
    </article> 
</section>
@{Html.RenderPartial("_Comments", Model.Comments);}

^ This is your post page razor view with a RenderPartial there for comments area.

@model IEnumerable<WhenToUseRenderActionAndRenderPartial.Models.Comment>
<section>
    <header>
        <h3>
            Comments</h3>
    </header>
    @foreach (var comment in Model)
    {
        <article>
            <header>
                @comment.Author on
                <time datetime="@comment.DateCreated.ToString("s") ">
                    @comment.DateCreated.ToLongDateString()
                </time>
            </header>
            <img alt="@comment.Author" src=@comment.ImageUrl />
            <p>
                @comment.Content
            </p>
        </article>
    }
</section>

^ This is your razor view for comments area.

And you can use the second view to render comments live (once 30 seconds). Ajax call to a method LiveController/BringLastCommentsfor PostId = 3 which will bring the comments and render only the comments partial view, not the entire page.

Some further reading here:

Render Partial http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/ (sweet diagrams)

Ajax http://kyleschaeffer.com/development/the-perfect-jquery-ajax-request/

Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54