Use Ajax to partially update the Razor page instance code, ajaxrazor
Razor is very powerful, but it cannot be refreshed. Therefore, it must be used with ajax.
This article provides a simple example to achieve local refresh through Razor and ajax.
First, create an MVC Project
Let's create a simple Controller Book
Add a view to it and add some simple Html code.
@ {ViewBag. Title = "Index"; Layout = null ;}<! DOCTYPE html>
Here I added a <div> and gave it an id = "partial". Then I click @ Html. actionLink will trigger the method in javascript and use ajax to access the path/Test/Book/Part/1. After the request is successful, the Html of my request will be constructed in <div>, this is actually the principle of partial refresh.
This is the Part code. Then we add a View to the Part.
The Part code can be very simple, and then we start our project
We keep clicking "Click me"
The page will change constantly.
Note that when using AJAX requests, the Url must be completely written. For example, my Controller is named Test in the Book folder under Area, and Action is Part, the Url is/Test/Book/Part and cannot be missing/Test. Otherwise, the following error occurs:
As you can see, if the Url is not fully written, the View returned by the Action is actually a wrong path.
Summary
The above section describes how to use Ajax to partially update the Razor page instance code. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!