In Core MVC, the method of passing values between controllers and views is very similar to that in ASP.
1. Weakly typed data: viewdata,viewbag
2. Strongly typed data: @model
Second, the code example
1.ViewData
public Iactionresult Index () { // Test when you right-click in the latest version of Cummuty2017 to add the encoding of the view file generated by the view For ANSI display garbled for Chinese // Use ViewData to implement values between controllers and views /span> /* * 1.ViewData Specify key-value pairs to set or read data * 2.ViewData Gets the result is an object if operations such as operation require a cast of * 3. The page title of the layout view uses the ViewData */
viewdata[ " menu_count "] = _context. Menu.count (); return View ();}
<class= "Alert Alert-danger"> <p> Total Current Menu: @ (viewdata["Menu_count"])</p> <P > Total Current menu: @ ((int) viewdata["Menu_count"]*1000)</p> </ Div >
2.ViewBag
public ActionResult Test1 () { // Use ViewBag to implement values passed between controller and view /* * 1. Same as viewbag use in ASP., is the dynamic type */ Viewbag.menu_count = _ Context. Menu.count (); return View ();}
<class= "Alert Alert-danger"> <p> Total Current Menu: @ (viewbag.menu_count)</p> <P > Total Current menu: @ (viewbag.menu_count*)</p></ Div >
[Email protected]
Public ActionResult Test2 () { // use @model mode to pass data list<menu> List = _context. Menu.tolist (); return View (list);}
@using Core_json.dba@model List<Menu>@{viewdata["Title"] = "Test2";}<H2>Test2</H2>@{ <Divclass= "List-group">@foreach (var item in Model) {<Divclass= "List-group-item">@item. MenuName<spanclass= "badge">@item. Addtime</span></Div> } </Div>}
More:
. The use and finishing of Efcore in Netcore
VS code build. Netcore Development Environment (II)
VS code build. Netcore Development Environment (i)
Value transfer between ASP. NET Core MVC controller and view