MVC 部分视图
1.contorller部分代码 public class HomeController : Controller { public ActionResult Index() { //默认返回的视图是与控制器同名的再Views文件夹下的Home文件夹下的Index.cshtml视图 return View(); } } 2.视图部分 的js调用加载视图到load div下 @{ ViewBag.Title = "Home Page"; } <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(function () { alert(1); $("#load").load("/Admin/UpdateAdmin"); }) </script> <div id="load"></div> 3.jquery load方法是走Contorller和Action的 所以会执行到这儿 namespace WebApplication15.Controllers { public class AdminController : Controller { public ActionResult UpdateAdmin() { //默认访问的视图是/Views/Shared/UpdateAdmin1.cshtml //return PartialView("UpdateAdmin1", new WebApplication15.Models.LoginModel() { Name = "admin", Password = "admin" }); //给出了具体访问的视图的相对路径 return PartialView("~/Views/Home/Contact.cshtml", new WebApplication15.Models.LoginModel() { Name = "admin", Password = "admin" }); } } 5.这里先给出默认路由设置 a. public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } b. public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 5.所以在地址栏输入http://localhost:51534/(默认路由就是http://localhost:51534/Home/Index)或者http://localhost:51534/Home/Index之后的结果
二.接下来简要说明下加载部分视图的最后两种形式 <div> action形式的加载:<br /> //第一个还是走action的 @*@Html.Action("UpdateAdmin1", "Admin")*@ //第二个不走aciton @Html.Partial("UpdateAdmin2", new WebApplication15.Models.LoginModel() { Name = "admin", Password = "admin" }) </div>