Moose TheTitan Moose TheTitan - 3 months ago 7
SQL Question

Rendering a View in another View (Not a partial view)

So I have a ReviewsController and an ActionResults which queries my table in my DB and return them in different views e.g.

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


And

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 View:

<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 View:

<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


So both of StudentWellnessReview and HAICUReviews Views display return their respective queries. However what I want to do is display these queries on one View. Your help me would be greatly appreciated.

Thanks in advance

Answer

To build on Kenneth's response:

You could add another action like so:

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

Then in StudentWellnessReviewsAndHAICUReviews.cshtml:

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