在另一个视图中渲染视图(不是局部视图)
Posted
技术标签:
【中文标题】在另一个视图中渲染视图(不是局部视图)【英文标题】:Rendering a View in another View (Not a partial view) 【发布时间】:2016-08-23 22:18:16 【问题描述】:所以我有一个 ReviewController 和一个 ActionResults 来查询我的数据库中的表并在不同的视图中返回它们,例如
在评论控制器中
public ActionResult StudentWellnessReviews()
using (var context = new SizaFakeEntities())
var userreview = context.Reviews.SqlQuery("select * from dbo.Review where WellnessService='Student Wellness Service'").ToList();
return View(userreview);
还有
public ActionResult HAICUReviews()
using (var context = new SizaFakeEntities())
var userreview = context.Reviews.SqlQuery("select * from dbo.Review where WellnessService='HAICU' ").ToList();
return View(userreview);
StudentWellnessReviews 视图:
<table class="table text-center width:50%">
<tr>
<td>
@html.ActionLink("Make Review", "Create", "Reviews", null, new @class = "btn btn-success btn-sm" )
@Html.ActionLink("Edit Posted Reviews", "ReviewEdit", "Reviews", null, new @class = "btn btn-info btn-sm" )
</td>
</tr>
@foreach (var item in Model)
<tr>
<td>
<h5>Username</h5>
</td>
<td>
<p><div align="left">@Html.DisplayFor(modelItem => item.Username)</div></p>
</td>
</tr>
<tr>
<td>
<h5>Wellness Service</h5>
</td>
<td>
<p><div align="left">@Html.DisplayFor(modelItem => item.WellnessService)</div></p>
</td>
</tr>
<tr>
<td>
<h5>Rating</h5>
</td>
<td>
<p><div align="left">@Html.DisplayFor(modelItem => item.Rating)</div></p>
</td>
</tr>
<tr>
<td>
<h5>Feedback</h5>
</td>
<td>
<p><div align="left"> @Html.DisplayFor(modelItem => item.Feedback)</div></p>
</td>
</tr>
<tr><td colspan="2"><hr class="active" /></td></tr>
</table>
HAICU评论视图:
<table class="table text-center width:50%">
<tr>
<td>
@Html.ActionLink("Make Review", "Create", "Reviews", null, new @class = "btn btn-success btn-sm" )
@Html.ActionLink("Edit Posted Reviews", "ReviewEdit", "Reviews", null, new @class = "btn btn-info btn-sm" )
</td>
</tr>
@foreach (var item in Model)
<tr>
<td>
<h5>Username</h5>
</td>
<td>
<p><div align="left">@Html.DisplayFor(modelItem => item.Username)</div></p>
</td>
</tr>
<tr>
<td>
<h5>Wellness Service</h5>
</td>
<td>
<p><div align="left">@Html.DisplayFor(modelItem => item.WellnessService)</div></p>
</td>
</tr>
<tr>
<td>
<h5>Rating</h5>
</td>
<td>
<p><div align="left">@Html.DisplayFor(modelItem => item.Rating)</div></p>
</td>
</tr>
<tr>
<td>
<h5>Feedback</h5>
</td>
<td>
<p><div align="left"> @Html.DisplayFor(modelItem => item.Feedback)</div></p>
</td>
</tr>
<tr><td colspan="2"><hr class="active" /></td></tr>
</table
因此,StudentWellnessReview 和 HAICUReviews 视图都显示返回各自的查询。但是我想做的是在一个视图上显示这些查询。非常感谢您对我的帮助。
提前致谢
【问题讨论】:
【参考方案1】:以肯尼斯的回应为基础:
你可以像这样添加另一个动作:
public ActionResult StudentWellnessReviewsAndHAICUReviews()
return View();
然后在 StudentWellnessReviewsAndHAICUReviews.cshtml 中:
@Html.Action("ReviewsController", "StudentWellnessReviews")
@Html.Action("ReviewsController", "HAICUReviews")
【讨论】:
顺便说一句,正确的使用方法是:@ Html.RenderAction("StudentWellnessReviews", "Reviews"); 无需添加词控制器【参考方案2】:你可以使用RenderAction
:
@Html.RenderAction("ReviewsController", "StudentWellnessReviews")
@Html.RenderAction("ReviewsController", "HAICUReviews")
【讨论】:
以上是关于在另一个视图中渲染视图(不是局部视图)的主要内容,如果未能解决你的问题,请参考以下文章