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

 

相关文章:

  • 2021-06-09
  • 2021-12-19
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2021-07-04
  • 2022-12-23
猜你喜欢
  • 2021-12-21
  • 2021-05-22
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案