[asp.net-mvc]只有一个 '模式' 语句允许在一个文件 (分页)

发布时间: 2016/10/9 2:51:33
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以我的索引视图呈现视图从 dbo 调用 StudentWellnessReviews 检索点评。审查表。一切都工作,直到我试着在 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:

问题是你不能为您的视图多次,右移 n ow 你说你的看法强类型与两个定义模型 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
官方微信
官方QQ群
31647020