Several Methods for ASP. NET mvc3 to call partialview

Source: Internet
Author: User

Our webpage usually consists of several parts. Some public areas are usually made into partialview, but these views are not completely fixed, therefore, a specific controller is needed to control it. Although static html can also be made into partialview, it is of little significance. The following shows how to call partialview in mvc3.

First, create a controller for testing:

Testcontroller

Public Class Testcontroller: Controller { Public Actionresult message () { VaR testobj = New List < String > () { "AAA" , "BBB" , "CCC" }; Return Partialview ( "Message" , Testobj ); } }

There is an action method, message (), and a partialview named "message" is returned, and a string is passed as a parameter to this view.

Generally, after a controller is created, right-click and add a view. By default, vs creates the corresponding View File in the Views folder. In this example, it should be "test/message. cshtml ". But we do not do this for partialview. Because it is a reusable view, it should be placed in the shared folder. MVC is the "Convention first" programming, so by default, the view under the shared folder and the view under the controller where the current page is located will be searched.

So we have to right-click the shared folder to add a view and select "create as a partial view". In this way, layout and so on will not be added.CodeIs a completely blank view.

 

Then, we need to do some HTML work based on the response type of the action. Here is a simple output of each string in list <string>.

Message. cshtml

@ Model list <String> <Ul>@Foreach (VAR itemIn Model){<Li> @ item </LI>}</Ul>

Finally, we can call this partialview in other views.

One way is to specify the controller and action:

<H3> call HTML. Action @ Html. Action ("Message","Test")

But the controller and action are not necessary. You can also create a new object and pass it to partialview:

<H3> call HTML. Partial @ Html. Partial ("Message",New List <String> {"CCC","DDD","Eee" })

Another common case is the script request. The following code calls test/message through jquery and fills the result in the DIV with the ID fuck:

< H3 > Use jquery </ H3 > < A Href = "Javascript :;" Onclick = "Loadfuck ()" > Invoke loadpartial </ A > < Div ID = "Fuck" > </ Div > < Script Type = "Text/JavaScript" > Function loadfuck (){ $ ("# Fuck"). Load ('/test/message '); } </ Script >

There is a picture with the truth:

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.