微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐