首页 > 代码库 > ASP.NET MVC传值

ASP.NET MVC传值

Controller


 1  public ActionResult Index()
 2         {
 3 
 4             ViewData["num"] = 5;
 5 
 6             ViewBag.count = 6;
 7 
 8             TempData["num"] = 7;
 9 
10             uname un = new uname() { name = "Chris", pwd = "123" };
11 
12             ViewData["uname"] = un;
13 
14             return View(un);
15         }

View


 1 @{
 2     Layout = null;
 3 }
 4 @using Demo.Models;
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8     <meta name="viewport" content="width=device-width" />
 9     <title>Index</title>
10 </head>
11 <body>
12     <div>
13         <h1>视图 Index.cshtml</h1>
14         <hr />
15         ViewData["num"]=@ViewData["num"];
16         <hr />
17         ViewBag.count=@ViewBag.count;
18         <hr />
19         TempData["num"]=@TempData["num"];
20         <hr />
21         @{
22             uname un = Model as uname;
23         }
24         @un.name-----@un.pwd;
25         <hr />
26         <hr/>
27         @{uname ul = ViewData["uname"] as uname;}
28         @un.name-----@un.pwd;
29     </div>
30 </body>
31 </html>

结果:

技术分享

ASP.NET MVC传值