Dynamically loading Partialview in an MVC application

Source: Internet
Author: User

Sometimes we don't want to put Partialview directly on HTML, but instead use jquery to load dynamically, or an event to load.

In order to demonstrate and do this exercise, we first in the views directory under the home to create _partial1.cshtml partial view, the view content of any custom, insus.net in this example only to display some text and a picture:



Next, we need to create a ActionResult () method, under the Controllers directory, to open the HomeController.cs:



Then go to the Views\home directory, create a dynamicload view, and in this view, practice dynamically loading the part of the view that you created at the beginning of the article.



In the view, Insus.net only writes a DIV tag, which will be loaded later partialview.

However, because Partialview (partial views) cannot be viewed directly as views (view). Coupled with dynamic loading, we need to specify the path to a partial view, so insus.net needs to add a method Partialviewresult () to the controller:



So we can go back to that dynamicload.cshtml view and write the jquery code:



When the view is running, you can see the following effect:

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.