zoukankan      html  css  js  c++  java
  • ASP.NET CORE 控制器传输view数据

    控制器:

     1 using Microsoft.AspNetCore.Mvc;
     2 using Student_mangent.Controllers.Models;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Threading.Tasks;
     7 
     8 namespace Student_mangent.Controllers
     9 {
    10     public class HomeController : Controller
    11     {
    12         private readonly IStudentRepository _studentRepository;
    13         public HomeController(IStudentRepository studentRepository)
    14         {
    15             _studentRepository = studentRepository;
    16         }
    17         public String Index()
    18         {
    19             return _studentRepository.GetStudent(1).Name; 
    20         }
    21         public IActionResult Details()
    22         {
    23             Student model = _studentRepository.GetStudent(1);
    24             ViewData["PageTitle"] = "Student Details";
    25             ViewData["Student"] = model;
    26             return View();
    27         }
    28     }
    29 }

    页面:

     1 @using Student_mangent.Controllers.Models
     2 <!DOCTYPE html>
     3 <html>
     4 <head>
     5     <title></title>
     6 </head>
     7 <body>
     8     <h3>@ViewData["PageTitle"]</h3>
     9     @{ 
    10         var student = ViewData["Student"] as Student;
    11     }
    12 <div>
    13     姓名:@student.Name
    14 </div>
    15 <div>
    16     邮箱:@student.E_mail
    17 </div>
    18 </body>
    19 </html>

     __________________________________第二版_______________________________________

    使用ViewBag:

    控制器:

     1 using Microsoft.AspNetCore.Mvc;
     2 using Student_mangent.Controllers.Models;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Threading.Tasks;
     7 
     8 namespace Student_mangent.Controllers
     9 {
    10     public class HomeController : Controller
    11     {
    12         private readonly IStudentRepository _studentRepository;
    13         public HomeController(IStudentRepository studentRepository)
    14         {
    15             _studentRepository = studentRepository;
    16         }
    17         public String Index()
    18         {
    19             return _studentRepository.GetStudent(1).Name; 
    20         }
    21         public IActionResult Details()
    22         {
    23             Student model = _studentRepository.GetStudent(1);
    24             ViewBag.PageTitle = "Student Details";
    25             ViewBag.Student = model;
    26             return View();
    27         }
    28     }
    29 }
    控制器

    页面:

     1 @using Student_mangent.Controllers.Models
     2 <!DOCTYPE html>
     3 <html>
     4 <head>
     5     <title></title>
     6 </head>
     7 <body>
     8     <h3>@ViewBag.PageTitle</h3>
     9 <div>
    10     姓名:@ViewBag.Student.Name
    11 </div>
    12 <div>
    13     邮箱:@ViewBag.Student.E_mail
    14 </div>
    15 </body>
    16 </html>
    页面
  • 相关阅读:
    C# 数据处理——(包括但不限)浮点数设置小数点后位数 (转)
    c#下怎么判断一个字符串是否可以转换为double类型(转)
    C++ string字符串按分隔符分割成一个数组(转)
    C#浮点数保留位(转)
    linq与lambda写法对照(转)
    C# DataTable 和List之间相互转换的方法(转)
    c#写入Mysql中文显示乱码 解决方法(转)
    微博营销之企业微博运营方案实用篇
    “最美店主”走红网络,或成国内欧美第一店
    月饼西施PK愤怒的小鸟,这个中秋狂掀DIY风
  • 原文地址:https://www.cnblogs.com/smartisn/p/14842886.html
Copyright © 2011-2022 走看看