This introduces the other parts of the common page, and also sets the title.
You can set up multiple common template sections by adding sections to the page.
@RenderSection ("Footer") defines a section that will error when the view does not provide code for this section: The section is undefined: "xxxxxx".
@RenderSection ("Footer", false) overloads, if the second argument is set to False, then this section is not required and will not be an error when the view does not provide the code for this section.
<! DOCTYPE html>
This is the view.
@{ Layout = "~/views/shared/_layout.cshtml"; Viewbag.title = "Set top caption"; Are you all right in your hometown? @section footer{ This is the footer section}
This is the entire page that will contain the common template, the body and the footer section.
When multiple pages of the page are used in the same layout, each page is assigned its layout through the Layouts property, resulting in redundancy, _viewstart.cshtml can be used to eliminate this redundancy, in the views directory another _ The viewstart.cshtml file, which takes precedence over execution of any view in the same directory, can be used to specify a default layout.
@{ Layout = "~/views/shared/_layout.cshtml";}
This layout is the default layout for all views, and you can override the Layout property in the view if you have views that you do not want to have. If you do not want the layout, set layout= "".
Third, partial viewThe partial view is, by definition, part of a view that allows programmers to separate parts of the view from the pages they need to reference. Can reduce code duplication and improve page code reusability.
For example: The following is an action that returns a partial view
Public ActionResult GetName () { return Partialview (); }
In any other view, just write the following code:
@Html. Partial ("GetName")
Or can be written like this
@{
Html.renderpartial ("GetName");
}
will be able to load the full code of the GetName view in the page. In general, the partial view should not include JS, or CSS because of this, is equal to the large view of the partial view of the JS and CSS, a good practice is the private JS and CSS through a section to introduce, so better. It is best not to have
@Html. Partial ("Partial View name")//have or the following three lines @{html.renderpartial ("Partial view name"); }
In addition, the partial view should pay attention to the overloads in @html.partial (), which are often useful, and if they are not, write their own extension methods. Take a look at the method signatures of these overloaded methods.
public static mvchtmlstring Partial (This htmlhelper htmlhelper, string partialviewname); public static mvchtmlstring Partial (This htmlhelper htmlhelper, string partialviewname, object model); public static mvchtmlstring Partial (This htmlhelper htmlhelper, string partialviewname, Viewdatadictionary viewData); C3/>public static mvchtmlstring Partial (This htmlhelper htmlhelper, string Partialviewname, object model, Viewdatadictionary viewData);
As you can see from the method signature, these are extension methods, @Html. Partial is the data entity and data dictionary that can be given to a partial view, and then the partial view can be regenerated into a view over the data passed through the main views.
Give an example: pass a model past the partial view, and then the partial view side will interpret the view based on the incoming data. Look at the controller code.
namespace mvcstart.controllers{public class Homecontroller:controller {public actionresult Index () { Man_model man = new Man_model (); Man. Id = 1; Man. Name = "Zhang Fei"; Man. age = All; return View (man); Public ActionResult GetName (object o) { Man_model man = o as Man_model; return Partialview (man); } } public class Man_model {public int Id {get; Set Public string Name {get; set;} public int Age {get; set;}} }
View Code: index.cshtml
@model mvcstart.controllers.man_model@model.id@model.age@html.partial ("GetId", model);
Getname.cshtml
@model mvcstart.controllers.man_model<div style= "line-height:1.5!important;" >> @Model .name</div>
The generated HTML code is:
The example is a bit far-fetched and does not see any effect, but really is to pass a man_model to the partial view through the main view, and then parse the returned HTML code by the partial views, what can this thing achieve? To give a simple example, if the code in the partial view is operated by the SEO staff, such as the page title, keywords, description, this value provides a box in the background to let the SEO staff settings, and the result is saved to the partial view of the code inside the words? So just pass in a model to the partial view, and the label of the partial view, such as Model.name, can automatically parse the real "Zhang Fei" data.
In 2013-5-5, there might have been some errors in understanding that the model can be passed to the partial view, but if a view loads a partial view, the partial view will have the same data as the data in the original page.
Let me give you an example of how
Controller code:
namespace mvcapplication1.controllers{public class Homecontroller:controller {public ActionResult Index () {person p = new Person (); P.id = 1; P.name = "Zhang San"; viewdata["abc"] = "Test ViewData"; Return View (P); } Public ActionResult indexpartial () { return View (); } } public class person {public int Id {get; set;} public string Name {get; set;}} }
Main View Code:
@model mvcapplication1.controllers.person@{ viewbag.title = "Index"; Layout = "~/views/shared/_layout.cshtml";} @Model. id@model.name@viewdata["abc"] = "123" @Html. Partial ("indexpartial");
Partial View Code:
@model mvcapplication1.controllers.person@{ viewbag.title = "indexpartial"; Layout = "~/views/shared/_layout.cshtml";} Contents of the <br/> partial view @model.name@model.id@viewdata[the contents of the "ABC"] partial view <br/>
Notice that the main view does not pass any data to the partial views, but the execution results are as follows, and you can see that the partial view can actually get the data from the main views:
Again to record a trick, such as a Web site in debugging with a test path, after uploading with a real domain name, this time we can write the domain name in the configuration file or XML file, run is read, when we can define a static property, this property returns the real domain name. You can have a @ namespace in all views. class. Static variables get a real domain name. In other words, some small, changeable fragments in the view can be obtained in this way.
In addition, it is important to note that methods such as html.renderaction () also support incoming parameters in the following ways:
@Html. Renderaction ("Left_nav", "Nav", new{parentid = 3});
How to call across regions:
@{html.renderaction ("Datagrid", "DataList", new {area = "Common"});
Parameter values can be obtained by adding a parameter actionresult (int parentid) directly inside the action. Area is intercepted by MVC and analyzed for cross-region invocation.
Difference:
Partial partial view
There are also html.renderpartial and html.renderaction, which are used to display a relatively separate block of functionality in a template page.
The same point of partial and renderpartial
Both are used to get a partial view.
The difference between partial and renderpartial
1. Partial has a return value (mvchtmlstring); renderpartial has no return value (Void).
A return value can be @html.partial ()
Without a return value directly @{html.partial ();}
Partial returns a string, so you can use a variable to receive the returned HTML string. But RenderPartial is writing the results into response, so there is no way to receive them with variables.
These two types of notation are equivalent.
The difference between action and renderaction
is used to introduce the code for an action, the difference is the same as the difference between partial and renderpartial above
The same point of the twoRenderPartial and renderaction are often used to display a relatively independent "block", such as a menu or a navigation bar. The results of both outputs are displayed as part of the view that is called.
Different points of the two
- Renderpatial's data comes from the calling view, and Renderaction comes from itself.
- Renderaction will initiate a new request, and renderpatial will not.
- The RenderPartial method is return Partialview (), and Renderaction is Return View ().