在 _Layout 侧边栏中动态显示 table
Dynamically display table in _Layout sidebar
我在 _Layout.cshtml
中有一个侧边栏,我想在其中动态显示来自另一个 view/model 的项目。我不太擅长数据库的东西,所以我需要一些帮助。
_Layout.cshtml
中的边栏:
<div id="sidebar">
@if (IsSectionDefined("SideBar")) {
@RenderSection("SideBar", required: false)
} else {
*Here is where I want the table to be displayed*
}
我想在侧边栏中显示的视图位于~/Views/Sidebarpics/index.cshtml
。
如果您需要更多代码来帮助我,请尽管询问!
谢谢
已解决,我的解决方案:
我复制了 SidebarPics/index 并将其命名为 index2.
_布局边栏:
<div id="sidebar">
@if (IsSectionDefined("SideBar"))
{
@RenderSection("SideBar", required: false)
}
else
{
@Html.Action("Index2", "SidebarPics")
}
家庭控制器:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
SidebarPicsController:
public ActionResult Index()
{
return View(db.SidebarPics.ToList());
}
[ChildActionOnly]
public ActionResult Index2()
{
return PartialView(db.SidebarPics.ToList());
}
这可能不是最好的解决方案,但对我来说效果很好,这样可以很容易地为它们设置不同的样式。
使用@Html.Partial 或@Html.Action 取决于您是否需要该视图通过控制器。
如果您只想加载 cshtml 而无需使用控制器操作,则将其呈现为 partial view:
@Html.Partial("~/Views/Sidebarpics/index.cshtml")
如果您需要通过控制器操作处理视图,则将其呈现为 child action:
@Html.Action("Index", "SidebarPics")
编辑:添加子控制器示例
编辑 2:我意识到我已经翻转了控制器和动作参数。行动是第一位的。
编辑 3:更新以根据评论更改部分索引的名称。
相关布局页面部分:
<div id="sidebar">
@if (IsSectionDefined("SideBar")) {
@RenderSection("SideBar", required: false)
} else {
@Html.Action("PartialIndex", "SidebarPics")
}
</div>
家庭控制器:
public class HomeController
{
[HttpGet]
public ActionResult Index()
{
return View();
}
}
SidebarPics 控制器:
public class SidebarPicsController
{
public ActionResult Index()
{
return View(db.SidebarPics.ToList());
}
[ChildActionOnly]
public ActionResult PartialIndex()
{
return PartialView("Index", db.SidebarPics.ToList());
}
}
我相信也可以省略 ChildActionOnly 属性,但这样您将有一个仅 returns 部分视图的操作。
您只需要在您的条件
中使用 @Html.Partial
调用部分视图
<div id="sidebar">
@if (IsSectionDefined("SideBar"))
{
@RenderSection("SideBar", required: false)
}
else
{
@Html.Partial("~/Views/Sidebarpics/index.cshtml")
}
</div>
有关局部视图的更多信息:http://mvc4beginner.com/Tutorial/MVC-Partial-Views.html
我在 _Layout.cshtml
中有一个侧边栏,我想在其中动态显示来自另一个 view/model 的项目。我不太擅长数据库的东西,所以我需要一些帮助。
_Layout.cshtml
中的边栏:
<div id="sidebar">
@if (IsSectionDefined("SideBar")) {
@RenderSection("SideBar", required: false)
} else {
*Here is where I want the table to be displayed*
}
我想在侧边栏中显示的视图位于~/Views/Sidebarpics/index.cshtml
。
如果您需要更多代码来帮助我,请尽管询问!
谢谢
已解决,我的解决方案:
我复制了 SidebarPics/index 并将其命名为 index2.
_布局边栏:
<div id="sidebar">
@if (IsSectionDefined("SideBar"))
{
@RenderSection("SideBar", required: false)
}
else
{
@Html.Action("Index2", "SidebarPics")
}
家庭控制器:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
SidebarPicsController:
public ActionResult Index()
{
return View(db.SidebarPics.ToList());
}
[ChildActionOnly]
public ActionResult Index2()
{
return PartialView(db.SidebarPics.ToList());
}
这可能不是最好的解决方案,但对我来说效果很好,这样可以很容易地为它们设置不同的样式。
使用@Html.Partial 或@Html.Action 取决于您是否需要该视图通过控制器。
如果您只想加载 cshtml 而无需使用控制器操作,则将其呈现为 partial view:
@Html.Partial("~/Views/Sidebarpics/index.cshtml")
如果您需要通过控制器操作处理视图,则将其呈现为 child action:
@Html.Action("Index", "SidebarPics")
编辑:添加子控制器示例
编辑 2:我意识到我已经翻转了控制器和动作参数。行动是第一位的。
编辑 3:更新以根据评论更改部分索引的名称。
相关布局页面部分:
<div id="sidebar">
@if (IsSectionDefined("SideBar")) {
@RenderSection("SideBar", required: false)
} else {
@Html.Action("PartialIndex", "SidebarPics")
}
</div>
家庭控制器:
public class HomeController
{
[HttpGet]
public ActionResult Index()
{
return View();
}
}
SidebarPics 控制器:
public class SidebarPicsController
{
public ActionResult Index()
{
return View(db.SidebarPics.ToList());
}
[ChildActionOnly]
public ActionResult PartialIndex()
{
return PartialView("Index", db.SidebarPics.ToList());
}
}
我相信也可以省略 ChildActionOnly 属性,但这样您将有一个仅 returns 部分视图的操作。
您只需要在您的条件
中使用@Html.Partial
调用部分视图
<div id="sidebar">
@if (IsSectionDefined("SideBar"))
{
@RenderSection("SideBar", required: false)
}
else
{
@Html.Partial("~/Views/Sidebarpics/index.cshtml")
}
</div>
有关局部视图的更多信息:http://mvc4beginner.com/Tutorial/MVC-Partial-Views.html