Asp.net MVC4 advanced programming learning notes-view learning lesson 1 20171009, mvc400001009
First of all, this article is just a learning note on the Learning Record of Asp.net MVC4 advanced programming. The book content is quite simple, but it is easy to forget, therefore, I took notes while watching. Maybe other friends may have no situation or logical order!
I. Three ways for the MVC controller to render the view.
Take the code in the HomeController controller as an example:
1. Default Mode
Public ActionResult Sample ()
{
ViewBag. Message = "MySampleView ";
Return View (); // note that the Home sub-directory under the Views directory is rendered (the Home sub-directory is found through Home in HomeController) the following view is named "action" and the value is "Sample (public ActionResult Sample ()", and the name of the action declared by HomeControll is "Sample"). The file name is "Sample. cshtml
2. Specify the view name
Public ActionResult Sample ()
{
ViewBag. Message = "MySampleView ";
Return View ("MySampleView"); // note that the View under the Home subdirectory under the rendering Views directory is named MySampleView, and the file name is MySampleView. cshtml.
}
3. Specify the view mode under different directories
Public ActionResult Sample ()
{
ViewBag. Message = "MySampleView ";
Return View ("~ /Views/Example/Index. cshtml "); // here is the view Index view under the Example sub-directory under the constant Views directory. The file name is Index. cshtml.
}
2. The controller transmits information to the view through ViewData and ViewBag.
ViewData is a dictionary data type.
Example:
ViewData ["CurrentTime"] = DateTime. Now;
ViewBag is a dynamic data type (dynamic ). The value assignment above is equivalent to the value assignment below.
Example:
ViewBag. CurrentTime = DateTime. Now;
Note: If the key in ViewData has spaces, ViewBag cannot be used for access. For example
ViewData ["full name"] cannot be converted to the ViewBag type. In addition, the value in ViewBag cannot be passed as a parameter because the data type is not determined, unless you use ViewData to pass Parameters or force data type conversion, for example
@ Html. TextBox ("name", ViewBag. name); this method is not suitable for compilation and needs to be changed to the following two forms,
@ Html. TextBox ("name", ViewData ["name"]);
@ Html. TextBox ("name", (string) ViewBag. name );
ViewData is a ViewDataDictionary type, not just a Dictionary type. It has a Model attribute that can be used to obtain the specified Model object in the view. For example, it is used to pass the specified model in the View:
Public ActionResult List ()
{
Var albums = new List <Album> ();
For (int I = 0; I <10; I ++)
{
Albums. Add (new Album {Title = "Album" + I })
}
Return View (albums );
}
In the background, you must first assign the parameter value in the View method to the ViewData. Model attribute. Then, inform the view of which type of model is being declared using @ model. Note that you need to enter a fully qualified type name (namespace and type name) for the model type, as shown below:
// Assign the parameter value to the ViewData. Model attribute
@ Model IEnumberable <MVCApplication1.Models. Album>
<Ul>
@ Foreach (Album p in Model)
{
<Li> @ p. Title </li>
}
</Ul>
If you do not want to output the complete type name, you can use using.
@ Using MVCApplication1.Models
// Assign the parameter value to the ViewData. Model attribute
@ Model IEnumberable <Album>
<Ul>
@ Foreach (Album p in Model)
{
<Li> @ p. Title </li>
}
</Ul>
For namespaces frequently used by views, you can configure <add namespace = "LYG. HelloWorld"/> in web. config under the View directory.
Example:
<System. web. webPages. razor>
<Host factoryType = "System. Web. Mvc. MvcWebRazorHostFactory, System. Web. Mvc, Version = 5.2.3.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Pages pageBaseType = "System. Web. Mvc. WebViewPage">
<Namespaces>
<Add namespace = "System. Web. Mvc"/>
<Add namespace = "System. Web. Mvc. Ajax"/>
<Add namespace = "System. Web. Mvc. Html"/>
<Add namespace = "System. Web. Optimization"/>
<Add namespace = "System. Web. Routing"/>
<Add namespace = "LYG. HelloWorld"/>
</Namespaces>
</Pages>
</System. web. webPages. razor>
Iii. View Model
When passing in a view through the MVC Model, there is often a master view with multiple slave views or other irrelevant information such as login information and user permission information. We can consider declaring the main view to @ model, and storing other information in ViewData. However, this is not suitable for everyone's development needs. to strictly control the inbound data of a view, all data must be of a strong type, so that the view writer can intelligently perceive it. The view model is introduced here to combine all the data into a view model and pass it into @ model. For example, the following shopping cart view model includes the product information of the main view, the total price of other information and sample code of the message displayed to the user.
Public class shoppingCatViewModel
{
Public IEnumberable <Product> products {get; set} // main view
Public decimal totalPrice {get; set;} // total price
Public string message {get; set;} // message displayed to the user
}
Then, use @ model shoppingCatViewModel to forcibly input a View to the model.
To this end, we will introduce the Razor syntax in the next lesson.