Use ajax to partially update the Razor page and ajax to partially update the razor page
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.
1 @ {2 ViewBag. Title = "Index"; 3 Layout = null; 4} 5 <! DOCTYPE html> 6
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.
This article introduces you to this topic. Thank you!