Value transfer between ASP. NET Core MVC controller and view

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.