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);
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM