Html.RenderPartial和Html.RenderAction的區別

添加一個PartialController控制器html

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MvcValidateDemo.Controllers
 8 {
 9     public class PartialController : Controller
10     {
11         //
12         // GET: /Partial/
13 
14         public ActionResult Index()
15         {
16             return View();
17         }
18 
19         public ActionResult About()
20         {
21 
22             ViewData["key"] = "Action  執行了";
23             ViewBag.Demo = "Action Bag執行了";
24             return View();
25         }
26 
27     }
28 }

添加About視圖spa

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>About</title>
11 </head>
12 <body>
13     <div>
14         About :@ViewBag.Demo+@ViewData["key"]
15     </div>
16 </body>
17 </html>

添加Index視圖code

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>Index</title>
11 </head>
12 <body>
13     <div>
14         @{
15             Html.RenderPartial("About");//把子視圖渲染到當前位置
16 
17             Html.RenderAction("About");//把Action的結果渲染到當前的位置
18         }
19     </div>
20 </body>
21 </html>
相關文章
相關標籤/搜索