1 Return to simple view
First create a new one. net mvc project, find the default HomeController. Index method in CS
public ActionResult Index() { return View(); }
There will be an Index in the Views folder with the same name as the method. Cshtml file, we will delete the default content, modify as follows:
@{ ViewBag.Title = "Home Page"; Layout = ""; } <h1>Small White GIS</h1>
Because of the default Index.cshtml has a default master page, the frame page, where we set it to not use the master page. The effect is as follows
The return View () method defaults to finding a view with the same name as the method name; of course, you can also create a new ActionResult method yourself, and then create a new cshtml file with the same name in the VIews folder, which will not be shown; About.cshtml and Contact.chtml have been automatically generated by projects already;
2 Returns the view of a normal object with a value passed
The above is just a return to the view, where you take an object back to the view and use it in the view;
2.1 First create a new User class under the Models folder
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication3.Models { public class User { public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } } }
2.2 HomeController. Models module was introduced in CS for the purpose of using its classes
using WebApplication3.Models;
2.3 Create a new ActionResult method GetUser as follows
public ActionResult GetUser() { User user = new User { Name = "Small White GIS", Age = 5, Sex = "male" }; return View(user); }
2.4 Create a new GetUser under the Views folder. Cshtml view
Or you can add a view directly by right-clicking the method above;
@model WebApplication3.Models.User @{ Layout = null; } <div> @Model.Name<br /> @Model.Agee<br /> @Model.Sexe<br /> </div>
Because ActionResult returns a User object, you can get the data from the Model by using the User object at the top as the global Model object for the current view; @ The model is followed by the project path where your User object is located. Replace it with your own. They are not a thing to declare with the model keyword and use the model object
The results are as follows:
3 Return the view of the passed value List
3.1 New ActioResult method GetUserList
public ActionResult GetUserList() { List<User> list = new List<User> { new User { Name = "Small White GIS", Age = 5, Sex = "male" }, new User { Name = "Small White GIS 1", Age = 6, Sex = "female" }, new User { Name = "Small White GIS 2", Age = 6, Sex = "male" } }; return View(list); }
3.2 New View
@model List<WebApplication3.Models.User> @{ Layout = null; } <div> @foreach (var user in Model) { @user.Name<br /> @user.Age<br /> @user.Sex<br /> <hr /> } </div>
Then start IIS Express to see the effect, remember that if you only modify the view, right-click the preview, and if you modify the Controller, you need to restart the IIS Express server
Here we point the Model object to the User List list, so we can use foreach to traverse the operation;
4 Return Partial View of the passed value
Partial view files, generally placed in the Share folder, because they can be shared as part of a page; Next, use the partial view on the Index front page and pass the value to the partial view
4.1 Begin by understanding how the lower part view is used
(1) Create a new view file CommonInfo under the Shared folder. Cshtml
<div style="color:purple;"> I'm a partial view that stores some shared information that can be used multiple times </div>
(2) Introduce @Html where partial view content needs to be rendered. Partial ("CommonInfo.cshtml");
@{ ViewBag.Title = "Home Page"; Layout = ""; } <h1>Small White GIS</h1> <hr /> <h1>The following is from a partial view file</h1> @Html.Partial("~/Views/Shared/CommonInfo.cshtml");
The results are as follows:
4.2 How to use ActionResult to return partial views and pass values
(1) In Index. Add Html where you want to render the partial view in cshtml. RenderAction(), as follows
@{ ViewBag.Title = "Home Page"; Layout = ""; } <h1>Small White GIS</h1> <hr /> <h1>The following is from a partial view file</h1> @*@Html.Partial("~/Views/Shared/CommonInfo.cshtml")*@ @{Html.RenderAction("GetCommonInfo")}
GetCommonInfo is the method name in HomeController;
(2) Add method GetCommonInfo
Method returns a PartialView, the first parameter is the name of the partitioned view, and the second parameter is the old Model value
public ActionResult GetCommonInfo() { User user = new User { Name = "Public Users", Age = 15, Sex = "male" }; return PartialView("CommonInfo", user); }
CommonInfo.cshtml
@model WebApplication3.Models.User @{ Layout = null; } <div> <div style="color:purple;"> I'm a partial view that stores some shared information that can be used multiple times<br /> @Model.Name<br /> @Model.Age<br /> @Model.Sex<br /> </div> </div>
As above, values can be passed to the partial view
So the two most common ways to render partial views are as follows: one directly renders the view, the other jumps to the view through the controller method, where values can be passed in between. A little more advanced