在另一个视图中渲染视图(不是局部视图)

本文关键字:视图 局部 另一个 | 更新日期: 2023-09-27 18:03:37

所以我有一个ReviewsController和一个ActionResults,它在我的DB中查询我的表,并在不同的视图中返回它们,例如

在ReviewsController

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>

HAICUReviews视图:

<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 Views显示返回各自的查询。然而,我想做的是在一个视图上显示这些查询。如果您能帮助我,我将不胜感激。

Thanks in advance

在另一个视图中渲染视图(不是局部视图)

以Kenneth的回答为基础:

你可以添加另一个动作,像这样:

public ActionResult StudentWellnessReviewsAndHAICUReviews()
{
    return View();
}

然后在StudentWellnessReviewsAndHAICUReviews.cshtml:

@Html.Action("ReviewsController", "StudentWellnessReviews")
@Html.Action("ReviewsController", "HAICUReviews")

您可以使用RenderAction:

@Html.RenderAction("ReviewsController", "StudentWellnessReviews")
@Html.RenderAction("ReviewsController", "HAICUReviews")