其实它和普通视图没有多大区别,只是创建分部视图的时候视图里没有任何内容,你需要什么标签你自己加。第二就是分部视图不会执行_ViewStart.cshtml中的内容)
控制器
PartialViewDeomController控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApp.Controllers
{
public class PartialViewDeomController : Controller
{
//
// 分部视图的作用一般用于嵌到如一些正常的视图中去。(类似与自定义控件)相当于: Server.Execute(string path)
public ActionResult PartialIndex()
{
//View()方法返回的视图默认都会去执行_ViewStart.cshtml中的内容
//return View();
//分部视图不会去执行_ViewStart.cshtml中的内容(分部视图以PartialView()返回)
return PartialView();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApp.Controllers
{
using MvcApp.Models;
public class PartialViewDeomController : Controller
{
//
// 分部视图的作用一般用于嵌到如一些正常的视图中去。(类似与自定义控件)相当于: Server.Execute(string path)
public ActionResult PartialIndex()
{
var list = new List()
{
new T_UserInfo(){Id=1,UserName="无盐海",Name="凡斌"},
new T_UserInfo(){Id=1,UserName="阿宝",Name="周晶"},
};
//分部视图不会去执行_ViewStart.cshtml中的内容(分部视图以PartialView()返回)
return PartialView(list);
}
}
}
@model List
PartialViewDeomController控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApp.Controllers
{
using MvcApp.Models;
public class PartialViewDeomController : Controller
{
public ActionResult Index()
{
return View();
}
// 分部视图的作用一般用于嵌到如一些正常的视图中去。(类似与自定义控件)相当于: Server.Execute(string path)
public ActionResult PartialIndex()
{
var list = new List()
{
new T_UserInfo(){Id=1,UserName="无盐海",Name="凡斌"},
new T_UserInfo(){Id=1,UserName="阿宝",Name="黄雪辉"},
};
//分部视图不会去执行_ViewStart.cshtml中的内容(分部视图以PartialView()返回)
return PartialView(list);
}
}
}
@{
Layout = null;
}
@using MvcApp.Models;
Index
@Html.Partial("PartialIndex", new List() { new T_UserInfo() { Id = 1, UserName = "无盐海", Name = "凡斌" }, new T_UserInfo() { Id = 1, UserName = "阿宝", Name = "周晶" }, })
@{
Html.RenderPartial("PartialIndex", new List() { new T_UserInfo() { Id = 1, UserName = "无盐海", Name = "凡斌" }, new T_UserInfo() { Id = 1, UserName = "阿宝", Name = "周晶" } });
}
@{Html.RenderAction("PartialIndex", "PartialViewDeom");}
@Html.Action("PartialIndex", new { controller = "PartialViewDeom" })
action