1. 程式人生 > >ASP.NET MVC傳值

ASP.NET MVC傳值

pub shtml action clas del layout count wid ctype

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"][email protected]["num"]; 16 <hr /> 17 [email protected]; 18 <hr /> 19 TempData["num"][email protected]["num"]; 20 <hr /> 21 @{
22 uname un = Model as uname; 23 } 24 @[email protected]; 25 <hr /> 26 <hr/> 27 @{uname ul = ViewData["uname"] as uname;} 28 @[email protected]; 29 </div> 30 </body> 31 </html>

結果:

技術分享

ASP.NET MVC傳值