Moose TheTitan Moose TheTitan - 2 months ago 8
C# Question

Only one 'model' statement is allowed in a file (Pagination)

So my Index view renders the view called StudentWellnessReviews retrieves reviews from the dbo.Review Table. Everything was working until I tried to setup pagination for the StudentWellnessReviews view in the ReviewsController. The error was:
Only one 'model' statement is allowed in a file.

ReviewsController

public class ReviewsController : Controller
{
private SizaData_1Entities db = new SizaData_1Entities();

// GET: Reviews
public ActionResult Index()
{
return View();
}

//Student Wellness Reviews
public ActionResult StudentWellnessReviews(int page = 1, int pageSize = 4)
{
using (var context = new SizaData_1Entities())
{

List<Review> listReviews = context.Reviews.SqlQuery("select * from dbo.Review where WellnessService='Student Wellness Service'").ToList();
PagedList<Review> userreview = new PagedList<Review>(listReviews, page, pageSize);
return View(userreview);
}
}


StudentWellnessReviews View:

@model IEnumerable<Siza.Models.Review>

@{
ViewBag.Title = "Index";
Layout = "";
}

@model PagedList.IPagedList<Siza.Models.Review>
@using PagedList.Mvc;


<table class="table text-center width:50%">
@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.TextAreaFor(modelItem => item.Feedback, new {@readonly = true})</div>
</p>
</td>
</tr>

<tr>
<td>
<h5>Date Created</h5>
</td>
<td>
<p>
<div align="left"> @Html.DisplayFor(modelItem => item.Date)</div>
</p>
</td>
</tr>
<tr><td colspan="2"><hr class="active"/></td></tr>

}

@Html.PagedListPager(Model, Page => Url.Action("StudentWellnessReviews",
new { Page, pageSize = Model.PageSize} ))
Showing @Model.FirstItemOnPage to @Model.LastItemOnPage of @Model.TotalItemCount Reviews

</table>


Part of Index view

@using Siza.Controllers


@{
ViewBag.Title = "Index";
}

@{Html.RenderAction("StudentWellnessReviews", "Reviews");}


Assistance would be greatly appreciated.

Answer

The problem is you cannot define model for your View multiple times, right n ow you are saying that your view is strongly typed with both IEnumerable<Siza.Models.Review> and PagedList.IPagedList<Siza.Models.Review> which cannot be, so what i see is you just need PagedList.IPagedList<Siza.Models.Review> so remove the top line where you are setting model to IEnumerable<Siza.Models.Review> to be like:

@model PagedList.IPagedList<Siza.Models.Review>
@using PagedList.Mvc;
@{
    ViewBag.Title = "Index";
    Layout = "";
}
.................
Your rest view
Comments