只有一个型号'文件中允许使用语句(分页)

时间:2016-09-19 19:58:44

标签: c# asp.net-mvc visual-studio razor

因此,我的索引视图呈现名为StudentWellnessReviews的视图从dbo.Review表中检索评论。一切正常,直到我尝试为ReviewsController中的StudentWellnessReviews视图设置分页。错误是:  只有一个型号'语句允许在文件中。

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查看:

    @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>

索引视图的一部分

    @using Siza.Controllers


@{
    ViewBag.Title = "Index";
}

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

非常感谢协助。

1 个答案:

答案 0 :(得分:3)

问题是你不能多次为你的View定义模型,右边你说你的视图是强类型的,IEnumerable<Siza.Models.Review>PagedList.IPagedList<Siza.Models.Review>都不能,所以我看到的是您只需要PagedList.IPagedList<Siza.Models.Review>,因此请将您将模型设置为IEnumerable<Siza.Models.Review>的顶行删除为:

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