The form element name in the interface form is mapped to the field name of the database table one by one (which table's data is the model of that table)
In the view page, you can specify which model the page belongs to
Note: The above relationships can be developed through MVC's strongly-typed view to address the IntelliSense of our development code
1. Steps for strongly-typed view development
1) Specify a strongly typed type (must be a reference type) in the page, code: Example:
@model list<mymvcbookshop.models.book>
2) from the controller to the strong type of data to the view view, then you must ensure that the incoming data will be consistent with the type defined in the strongly typed view, for example:
Mybookshopcontext bookshop = new Mybookshopcontext (); Public ActionResult Index () { list<book> m = bookshop. Books.orderby (p = p.id). Take (16). ToList (); Return View (m); }
3) In the View page, apply its Model property, for example:
@{foreach (var item in Model) {<li><a href= "/book/[email protected]" > @item. Title</li>}}
2. Several methods for transferring data to the interface of controller
1) pass through strongly typed
2) pass through the ViewBag, it is the dynamic parsing type, at run time will automatically parse what type, when writing does not know the type
3) passing through ViewData, it is a collection of ways to store (in the form of key-value pairs stored), in the interface using the first conversion after use, for example:
In the controller Mybookshopcontext Bookshop = new Mybookshopcontext (); [HttpGet] Public ActionResult Index () { list<category> listcstegory = bookshop. Categories.orderby (p = p.id). ToList (); viewdata["Category"] = listcstegory; return View (); } On the view page <ul class= "Black" > @{ list<category> listcate = viewdata["Category"] as list< category>; foreach (var item in listcate) { <li><a href= "/book/list/@item. Id "> @item. Name</a></li> }}</ul>
4) Pass data via TempData (typically used primarily for data transfer between different actions), TempData only once, and store data in key-value pairs
The return value is: Return Redirecttoaction ("Another action name") so that TempData can only read the data once
Summarize the difference between ViewBag,ViewData,tempdata ?
5.MVC Framework Development (strong-type development, controller-to-interface transfer of data to several methods)