Use ajax to partially update the Razor page and ajax to partially update the razor page

Source: Internet
Author: User
Tags actionlink

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!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.