how to reload view component and not the rest of the page

lawrence whittemore asked on June 15, 2021 17:11

Not sure if this is something someone can help me on. But I have a view component that pulls in a list of articles and a form based filter. Trying to figure out how to not refresh the whole page when the form is submitted. Any advice would be greatly appreciated.

Recent Answers


Jake Kula answered on June 17, 2021 09:01

Hi Lawrence,

One technique I often use is an AJAX call.

Your view component will have JS that calls a Web API end-point within your application. The Web API retrieves your data, probably based on the filter values you will pass.

Depending on how your application is set up, this should be one method that should achieve what you need.

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on October 12, 2021 19:02

how would i write the ajax call to target an action on the home page controller when the home page controller uses kentico's tree based routing?

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on October 12, 2021 21:40

so I have this and I am getting the view component code, but nothing is refreshing.

 $j.ajax({
        url: "@Url.Action("RefreshQuickLinks")",
            method: "GET",
            success: function () {
                $j("#QuickLinks").html();
            }
        });
    };
0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on October 12, 2021 21:42

here is some more info if that helps home page controller has this

 public IActionResult RefreshQuickLinks()
    {
        // needs to not have ViewComponent in title
        return ViewComponent("QuickLinks");
    }

Home page view has this

<div id="QuickLinks">
                @(await Component.InvokeAsync("QuickLinks"))
            </div>
0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on October 12, 2021 21:51

Also, some context to what is going on. Before I call this

$j.ajax({
    url: "@Url.Action("RefreshQuickLinks")",
        method: "GET",
        success: function () {
            $j("#QuickLinks").html();
        }
    });
};

I have already updated the database, so I just need a refresh of the information. No parameters or anything are being passed into the view component from the javascript.

0 votesVote for this answer Mark as a Correct answer

lawrence whittemore answered on October 12, 2021 22:15

so... This works.... but I don't udnerstand what data does when I am not using it for anything...

 $j.ajax({
        url: "@Url.Action("RefreshQuickLinks")",
            data: { test: 'test' },
            method: "GET",
            success: function (data) {
                $j("#QuickLinks").html("");
                $j("#QuickLinks").html(data);                                        
            }
        });
    };
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.