添加一个PartialController控制器
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视图
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 :@[email protected]["key"] 15 </div> 16 </body> 17 </html>
添加Index视图
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>
时间: 2024-11-05 10:06:08