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 item In 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: