ASP.NET MVC 分部视图
@model PartViewDemo.Models.HomeInfo
@using PartViewDemo.Models;
@{
ViewBag.Title = "Index";
}
@if (Model != null)
{
<p>@Model.Name</p>
<p>@Model.Content</p>
}
@*通过Controller获取数据源*@
@Html.Action("PartView", "Home")
@*通过自己对model注入数据源*@
@Html.Partial("Index2", new HomeInfo { Name = "Part5", Content = "PartContent5" })
一般在View中有上面两种写法。
在Controller中PartialViewResult 和ActionResult做区分,其实作用相同,可用ActionResult代替PartialViewResult
返回view时候,可以指定View的名称。
public ActionResult Index()
{
HomeInfo data = new HomeInfo { Name = "Home", Content = "Home Content" };
return View(data);
}
public ActionResult Index2()
{
HomeInfo data = new HomeInfo { Name = "Home", Content = "Home Content" };
return View(data);
}
public ActionResult PartView()
{
PartInfo info = new PartInfo { Name = "Part10", Content = "Part Content10" };
return PartialView("PartView2", info);
}
public PartialViewResult PartView2()
{
PartInfo info = new PartInfo { Name = "Part3", Content = "Part Content3" };
return PartialView(info);
}