Re-render razor partial view returned from AJAX call

Martin Shishkov

I have the following javascript:

            $.ajax({
                type: "POST",
                url: url,
                data: $(form).serialize(),
                success: function (res) {
                    commentsField.append(res);
                }
            });

this ajax call is receiving a partial view from the controller successfully. The problem is that there is razor syntax in the received partial view which renders correctly on page refresh. Is there a way to re-render only this new chunk of cshtml code without refreshing the whole page?

UPDATE:

This is my view, it is a DisplayTemplate for a comment model:

@model CommentModel

<div class="comment">
@Model.Body &#8212; <span class="buttonlink">@Model.AuthorUsername</span>; @Model.PostDate.ToString("F", CultureInfo.CreateSpecificCulture("bg-BG"))
    @if (ViewBag.UserId != null && ViewBag.UserId.Equals(Model.AuthorId))
    {
        <a class="comment-delete-btn" href="javascript:{}" data-parentid="@Model.ParentId" data-id="@Model.Id"><span class="fa fa-times-circle pull-right"></span></a>
    }
</div>

And my issue is that the html inside the if statement is not showing right after retrieval - I have to refresh the page in order to render the <a> tag.

MRebati

I think your ViewBag.UserId was not set in your PatialView. It's null then the If statement does not execute. that causes the problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mvc4 Razor Ajax Call to show partial view

From Dev

Render a partial everytime on ajax call

From Dev

Not able to refresh the partial view after a form submission from another partial view within it, using ajax in mvc razor

From Dev

Main view get called again after a partial view has been returned through ajax call

From Dev

Render a partial view via ajax in Ruby on Rails

From Dev

How to render partial view in MVC5 via ajax call to a controller and return HTML

From Dev

Render partial from controller and from view

From Dev

Calling a partial view from AJAX

From Dev

How to create partial view via AJAX call

From Dev

Render partial for collection from a shared view directory

From Dev

render partial view from jquery not working

From Dev

Render form partial from view ROR

From Dev

Render a partial view from another controller

From Dev

How do I render a View in Durandal with data returned from a jsonp call?

From Dev

Simplest way to get model data/objects in the view from input with an ajax partial view call(s)

From Dev

C# MVC 5 Razor: Updating a partial view with Ajax fails

From Dev

Render modal partial view via ajax in specified div block

From Dev

Using Ajax to render partial view, taking an object as parameter

From Dev

Render Partial View to Ajax request Zend framework 3

From Dev

MVC: How to get controller to render partial view initiated from the view

From Dev

how to call the partial view from script function?

From Dev

How to hide but still render e.g. a partial view in MVC Razor View?

From Dev

Render a partial view with sailsjs

From Dev

How to render partial view?

From Dev

How to call Javascript from Razor View

From Dev

call mvc razor view from javascript function

From Dev

AJAX Render Partial

From Dev

AJAX Render Partial

From Dev

ajax posting from a partial view to controller is null

Related Related

  1. 1

    mvc4 Razor Ajax Call to show partial view

  2. 2

    Render a partial everytime on ajax call

  3. 3

    Not able to refresh the partial view after a form submission from another partial view within it, using ajax in mvc razor

  4. 4

    Main view get called again after a partial view has been returned through ajax call

  5. 5

    Render a partial view via ajax in Ruby on Rails

  6. 6

    How to render partial view in MVC5 via ajax call to a controller and return HTML

  7. 7

    Render partial from controller and from view

  8. 8

    Calling a partial view from AJAX

  9. 9

    How to create partial view via AJAX call

  10. 10

    Render partial for collection from a shared view directory

  11. 11

    render partial view from jquery not working

  12. 12

    Render form partial from view ROR

  13. 13

    Render a partial view from another controller

  14. 14

    How do I render a View in Durandal with data returned from a jsonp call?

  15. 15

    Simplest way to get model data/objects in the view from input with an ajax partial view call(s)

  16. 16

    C# MVC 5 Razor: Updating a partial view with Ajax fails

  17. 17

    Render modal partial view via ajax in specified div block

  18. 18

    Using Ajax to render partial view, taking an object as parameter

  19. 19

    Render Partial View to Ajax request Zend framework 3

  20. 20

    MVC: How to get controller to render partial view initiated from the view

  21. 21

    how to call the partial view from script function?

  22. 22

    How to hide but still render e.g. a partial view in MVC Razor View?

  23. 23

    Render a partial view with sailsjs

  24. 24

    How to render partial view?

  25. 25

    How to call Javascript from Razor View

  26. 26

    call mvc razor view from javascript function

  27. 27

    AJAX Render Partial

  28. 28

    AJAX Render Partial

  29. 29

    ajax posting from a partial view to controller is null

HotTag

Archive